I have written some coding that has a GUI that has an enter button that, when clicked, opens another GUI that I have created. The GUI that gets displayed after has also 2 buttons. One of them does the right thing and i want the second button to close the GUI, but ONLY the second one, so the first one should remain on screen. I used System.exit(0) but that closes bouth GUIs. Does anayone know what to write to close only one GUI ?
-
What do you mean by "opens another GUI"? Do you mean it opens a new window? Is that window a JFrame? – Code-Apprentice Apr 19 '17 at 18:52
-
1Instead of trying to describe your code in English only, post the relevant code here. – takendarkk Apr 19 '17 at 18:53
-
By GUI, I assume you mean JFrame. JFrame has a setVisible() method. – JB Nizet Apr 19 '17 at 18:53
-
Along with your English description, you should post some code. You do not need to post your entire program. Just the code that is relevant to your question. To make this easier, you can start a new project and write code that duplicates the parts of your main app which you need help with. – Code-Apprentice Apr 19 '17 at 18:54
-
1@Code-Apprentice The best form of presented code: a [mcve]. And a tip: `[mcve]` in a comment auto expands to [mcve]. Less typing. – Andrew Thompson Apr 20 '17 at 06:03
-
1@AndrewThompson I know about that page in the help section but did not know about the shortcut syntax for the link. Thank you. – Code-Apprentice Apr 20 '17 at 13:53
-
@Code-Apprentice You're welcome. I like to offer that tip to people I deem helpful (who provide more 'signal' than 'noise' - which is also something I try to achieve!). – Andrew Thompson Apr 20 '17 at 16:14
2 Answers
when clicked, opens another GUI that I have created.
The second "GUI" should be a JDialog, not a JFrame.
I used System.exit(0) but that closes bouth GUIs.
Instead you should use:
dialog.dispose();
so you will need a reference to the dialog. You can get a reference to the current window by using:
JButton button = (JButton)e.getSource();
Window dialog = SwingUtilities.windowForComponent( button );
Where the getSource()
method is from the ActionEvent
class of your ActionListener
.
Or a better way is to simulate that the "Close" button of the dialog was clicked. Check out Closing an Application. The ExitAction
in this link will dispatch an event to the dialog.
Note: you don't need to use the "CloseListener" from the link, only the ExitAction
and then make sure you use the setDefaultCloseOperation(...)
when you create the dialog.

- 321,443
- 19
- 166
- 288
I assume you are talking about JFrames when you say GUI. JFrame has a method called setVisible().
https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setVisible(boolean)
You can use setVisible(false) in that button and it should close the second JFrame

- 23
- 5
-
Don't use a second JFrame. Child windows should be a JDialog. See: http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice – camickr Apr 19 '17 at 19:16
-
I am assuming he opened another JFrame when he says GUI but yes you are correct JDialogs should be used. – Ibrahim Naazir Apr 19 '17 at 19:19