1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

4

I have a program with 2 classes that each create 1 window.

By tagging 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 JFrames 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.

Community
  • 1
  • 1
user1803551
  • 12,965
  • 5
  • 47
  • 74
0

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.

Community
  • 1
  • 1
Nickal
  • 669
  • 2
  • 11
  • 25