How to stop JOptionPane from disabling root frame

599 views Asked by At

How do I show a JOptionPane without disabling any currently open frames?

I have a JFrame with a Start and Stop button. At any point, I want the user to be able to press the 'Stop' button on the main frame, to stop a secondary Thread (which was started by the Start button).

However the second running Thread sometimes opens a JOptionPane. When it does so, the main frame is disabled, and the user can't press the stop button.

(And when it opens multiple of these JOptionPanes in a row, it becomes immensely frustrating trying to stop it).

I've tried

JOptionPane.showMessageDialog(null, "It's Broken Mate");

With no success. I also tried passing it a JFrame to disable:

JOptionPane.showMessageDialog(new JFrame(), "Still No Go");

That failed too. I even tried

JFrame frame = new JFrame();
frame.setVisible(true);
JOptionPane.showMessageDialog(frame, "CMON Please");

And even furthermore,

JFrame frame = new JFrame();
frame.setVisible(true);
JOptionPane.setRootFrame(frame);
JOptionPane.showMessageDialog(frame, "I'm getting angry now.");

And STILL. Nothing. Does not work. It opens up a message box, and everything is disabled. I have to dismiss the JOptionPane before anything becomes enabled again...

Any ideas?

Cheers

1

There are 1 answers

0
Xyene On BEST ANSWER

JOptionPanes are modal, so you can't do this directly.

All dialogs are modal. Each showXxxDialog method blocks the caller until the user's interaction is complete.

You'll have to use a JDialog or similar.

    JOptionPane p = new JOptionPane("I'm getting happy now.");
    JDialog k = p.createDialog(":-)");
    k.setModal(false); // Makes the dialog not modal
    k.setVisible(true);