File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Can't close JOptionDialog box by clicking OK Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can Watch "Can New topic
Author

Can't close JOptionDialog box by clicking OK

Sarone Thach
Ranch Hand

Joined: Jun 25, 2003
Posts: 89
Hey guys,
Can someone see what is wrong with my code. I can't close the dialog box by clicking the "OK" button, but I can close it by clicking the X at the top right(For windows) or top left for Mac.
Weird!
Heres the code
public static void showInfoDialog(int iError)
{
final String sError1 = "Error1...";
final String sError2 = "Error2...";
switch(iError)
{
case 0:
new Thread(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, sError1, "Server Error", JOptionPane.ERROR_MESSAGE);
}
}).start();
break;
case 1:
new Thread(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, sError2, "Server Error", JOptionPane.ERROR_MESSAGE);
}
}).start();
break;
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I can't see anything wrong with what you've written, so I suspect that there's a deadlock on the event thread. If you call this function from an event handler, but that event handler hasn't returned by the time you want to click the button, then the event thread (the thread that would handle the OK button click) is still tied up running your event handler, so the button does nothing. So a handler like this could cause behavior something like this:


[Jess in Action][AskingGoodQuestions]
Sarone Thach
Ranch Hand

Joined: Jun 25, 2003
Posts: 89
Thread.sleep() only makes it worst, the whole program does not respond to any of my actions. Actually the text and the message does not get displayed in the dialog box.
I have no idea how to fix this deadlock issue.
I tried creating an Instance of JOptionPane, then invoking the showMessageDialog(), got no thread issues.
JOptionPane optionPane = new JOptionPane(sMsg);
optionPane.showMessageDialog(m_frame, sError, "Server Error", JOptionPane.ERROR_MESSAGE);
Not what I wanted, but at least it works.
I still want to understand what went wrong with the previous approach, and how to fix it, as I would like to develop an understanding for threads.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Can't close JOptionDialog box by clicking OK
 
Similar Threads
Java exception
Help correcting a button behavior in swing
Display an array in a swing type box
Threads
problem with showMessageDialog