• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can't close JOptionDialog box by clicking OK

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic