I have one main JFrame
and one secondary JFrame
. Now i want to close one JFrame
but when I close that JFrame
the main JFrame
also gets closed. But I don't want that to get closed, so how do I stop it in java?
Asked
Active
Viewed 37 times
0

Andrew Thompson
- 168,117
- 40
- 217
- 433

ahad nadeem
- 11
- 3
-
2Relevant: [The Use of Multiple JFrames: Good or Bad Practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) -TL;DR: It is bad practice and not user friendly. So instead of two `JFrame`s, depending on your use-case 1) use a modal `JDialog` to display intermediate information, or 2) use a `CardLayout` to switch between context in your `JFrame`. – maloomeister Nov 17 '21 at 08:12
-
1You probably should show your `close` methods. Maybe you have somewhere a `System.exit(..)`? – jmizv Nov 17 '21 at 08:12
-
2Pssst.. the secret lies in the default close operation used for the frames in the code not shown. But follow the advice of @maloomeister, since there never should *be* two frames. – Andrew Thompson Nov 17 '21 at 08:30
1 Answers
0
there's a function in JFrame
called setDefaultCloseOperation(...)
You can do:
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)
which will set the visibility of the secondary frame to false
instead of exiting the program

Arthur
- 9
- 4