Search code examples
javauser-interfacejpaneljoptionpane

JDialogBox dont display content intermittently


Good evening stackoverflow. I have a problem involving JOptionPanes. Every so often they simply do not display the content that i give it. And here to illustrate my problem: This is what it should look like: enter image description here

and every so often, this is what it displays: enter image description here

heres the function call to build the thing. "panel" is just a simple JPanel with the widgets added.

int a = JOptionPane.showConfirmDialog(null,panel,"Please enter result details",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);

The thing with this that is driving me crazy is that it's intermittent so i have no idea what caused this. Any ideas?


Solution

  • You should ensure that you are calling your JOptionPane updating code on the Event Dispatching Thread. Swing components should be updated on the EDT to ensure they are repainted properly, most issues with incorrect updating are because it is not being called on the EDT.

    If you are ever unsure about whether or not you are on the EDT, SwingUtilities.isEventDispatchThread() is a good debugging tool.

    Edit: I missed that there was a comment that mentioned this, sorry.