Search code examples
javadialogjoptionpanejdialog

Why JDialog doesn't close after JOptionPane showMessageDialog?


When my server app is starting, a JDialog opens to indicate to the user that the app is loading. But between the opening of JDialog and its closing, I use JOptionPane.showMessageDialog() to display another message.

The problem is, if I display this new message before closing JDialog then JDialog will never close even if I close JOptionPane dialog. If I remove the JOptionPane dialog then the JDialog closes as usual.

Why opening JOptionPane.showMessageDialog() disable JDialog closing ?

Is use this code to open JDialog:

final JDialog dlg = new JDialog(this, "Veuillez patienter, le serveur démarre...", true);
dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlg.setSize(300, 75);
dlg.setResizable(false);
dlg.setLocationRelativeTo(this);         
Thread t = new Thread(() -> {
    dlg.setVisible(true);
});
t.start();

And this code to close it:

dlg.setVisible(false);

And between these lines of code I do this to open message dialog:

JOptionPane.showMessageDialog(this, String.format(I18n.i18n.getString("PopupWifiCreated"), this.SSID, this.password), null, JOptionPane.INFORMATION_MESSAGE);

Anyone has an idea?

Thanks.


Solution

  • In my opinion setting the visibility of any frame to false is not the greatest idea. Of course it depends what are your goals, but I'd still not recommend it. It should be done like e.g.:

        dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    
        dlg.close();
    or
        dlg.dispose();
    

    If it still wont work, try to reverse the order of dialogs - first show messageDialog, and then JDialog.