Mats_SX
Mats_SX

Reputation: 194

JFrame in Java

I am using a JFrame as a pop-up from a main frame, where the user is supposed to enter some information and then press OK. But the problem is that once this sub-frame has opened up, I want the program to ignore any other signal until the OK button is pressed, similar as how it is done with JOptionPanes. As it is now, you could click the button to open up the sub-frame several times, thus getting several frames, and this is not the intention (it causes a lot of bugs even).

I could solve it with a boolean that is true once the OK button is pressed, but that doesn't seem like a cool way to fix it. I feel there has to be some other way of doing it, seeing as I can see the sought-after effect coming from my JOptionPanes, but can't get to it.

Cheers.

Upvotes: 5

Views: 1066

Answers (2)

jjnguy
jjnguy

Reputation: 138874

Just change the JFrame to a JDialog.

Then in the constructor use this line:

setModal(true);

Your app will then block until the dialog box is closed.

You might want to look into a JOptionPane.showInputDialog() if your users are simply entering a String.

Upvotes: 6

Michael Myers
Michael Myers

Reputation: 191905

You need a JDialog with modal = true. From the Javadocs:

modal - specifies whether dialog blocks user input to other top-level windows when shown.

Upvotes: 10

Related Questions