I have code with the class MainFrame
, and the this
is a mainframe object. So here's my problem. When I do this.setDefaultCloseOperation(DISPOSE_ON_CLOSE)
, it closes out of the dialog, but not the application. I know I should be using EXIT_ON_CLOSE
, but that only works for a JFrame
, and not a MainFrame
object. I've tried work arounds using System.exit(0)
, but they all either exit at the wrong time, or fail all together. Is there anyway in which I can access the "X" button in the corner of the frame, and set it so that when it is pressed, it performs a certain action?
Asked
Active
Viewed 245 times
-1

stevecross
- 5,588
- 7
- 47
- 85

Peter Barnett
- 201
- 1
- 2
- 13
-
1Show some code. How does your `MainFrame` class look like? – stevecross Apr 10 '14 at 19:58
-
In case you extend a `JDialog`: Try `JDialog.EXIT_ON_CLOSE`. – stevecross Apr 10 '14 at 20:01
-
1*....access the "X" button in the corner of the frame* YES, [`JFrame#addWindowListener`](http://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html), see [`this`](http://stackoverflow.com/questions/7191330/closing-a-java-program-properly-when-jdialog-is-the-main-window) link also – Azad Apr 10 '14 at 20:01
-
*"I know I should be using EXIT_ON_CLOSE, but that only works for a JFrame"* - What proof have you got for this? Have you tested it? – MadProgrammer Apr 10 '14 at 20:48
-
Ummm...yes I have actually MadProgrammer. It's actually a direct call only from the JFrame API. – Peter Barnett Apr 11 '14 at 01:25
2 Answers
0
Azad's comment worked! Go to the link as described.
EDIT: Better yet, lemme post the correct idea here in case that question ever disappears.
dialog.addWindowListener(new WindowAdapter() {
@Override public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
Where dialog is your JDialog with all the info. I recommend posting it at the end of your method where your JDialog is defined and used.

Peter Barnett
- 201
- 1
- 2
- 13
-
1Why don't you just write `dialog.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE)`? – stevecross Apr 10 '14 at 20:25
-
JDialog has to be a "DISPOSE_ON_CLOSE". Only a JFrame can do a EXIT_ON_CLOSE. – Peter Barnett Apr 11 '14 at 01:24
0
it closes out of the dialog, but not the application.
Why are you trying to exit an application from a dialog? An application should only close through its JFrame.
Is there anyway in which I can access the "X" button in the corner of the frame, and set it so that when it is pressed, it performs a certain action?
See Closing an Application for ideas on the subject as well as code examples.

camickr
- 321,443
- 19
- 166
- 288
-
Because when I tried throwing it into a JFrame, I was getting crazy, poorly-formatted GUIs that were perfect in a JDialog. – Peter Barnett Apr 11 '14 at 01:26
-
@user3010468, The layout in a JFrame or JDialog is identical. Your code is the problem, not the JFrame. That is why you always create a GUI by adding components to a panel and then add the panel to the frame. I suggest you fix your code. – camickr Apr 11 '14 at 03:20