I have a program with 2 classes that each create 1 window. The first window has a button that opens the second one and I wanted a way to check when the second window is opened so that the user can't close the first one.
-
1Please, fix the typo in the title and add the Java code you are using, stripping it of any unnecessary methods. – Agostino Dec 19 '15 at 07:53
-
1Using isVisible() should help you. – Arnaud Dec 19 '15 at 07:57
2 Answers
I have a program with 2 classes that each create 1 window.
By tagging jframe I'm going to assume that those are what you call "window".
The first window has a button that opens the second one
You don't want mutiple JFrame
s in your application. What you want is a JDialog
(or a JOptionPane
).
and I wanted a way to check when the second window is opened so that the user can't close the first one.
What you actually want is a modal dialog. Generally speaking, ff the dialog is modal, you can't interact with its parent while it is open.
So when you press the button in frame1
, you will want to call the following constructor:
JDialog(Dialog owner, boolean modal)
with frame1
and true
. JOptionPane
is modal by default. See the tutorial.

- 1
- 1

- 12,965
- 5
- 47
- 74
While closing the frame, use JFrame.dispose()
function instead of System.exit() and you may also use JFrame.getFrames()
or JFrame.getWindows()
to get references of all the active windows and exit if the count is zero.
For more understanding you may visit this question too.