• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't close JOptionDialog box by clicking OK

 
Sarone Thach
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Sarone Thach
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic