Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JCheckBox and ItemListener  RSS feed

 
Markus Schmider
Ranch Hand
Posts: 148
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to learn Swing for a project.
I have tried the following code from a tutorial. It does not work like I expected. When I click the checkbox, I get both MessageDialogs.
After I cofirm both, the checkbox is still unchecked.
What is going on here?

 
Tony Docherty
Bartender
Posts: 3264
81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why this is happening but by displaying the JOptionPane from within the listener the checkbox is being deselected which is why you get the second JOptionPane being displayed. You can get around this by wrapping the code to display the JOptionPane in a Runnable and putting it onto the Event Queue to be invoked later. eg:


Hopefully someone else can give an explanation as to why this occurs and we can both learn something.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!