8

I have a JDialog as the main window in my application (originally it was a JFrame but it showed in the taskbar which I didn't want).

Currently I am doing:

setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

and when I click an exit button:

frame.dispose();

But the process still seems to hang around in the background

JFrame had JFrame.EXIT_ON_CLOSE which seemed to do what I wanted.

How can I close my application properly?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
jax
  • 37,735
  • 57
  • 182
  • 278

6 Answers6

19

You need to add a WindowListener that will do System.exit(0) when the dialog closes.

JDialog dialog = ...;
dialog.addWindowListener(new WindowAdapter() { 
    @Override public void windowClosed(WindowEvent e) { 
      System.exit(0);
    }
  });

Of course, the System.exit(0) after you hit the Exit button (that was suggested elsewhere in this thread) is still needed.

Itay Maman
  • 30,277
  • 10
  • 88
  • 118
  • 1
    This is it. In my case, we have ` JPanel ` extended wiith ` JDialog ` so I was not able to call ` addWindowListerner ` directly. But since we extended our dialog with `JDialog `, I was able to call ` super.addWindowListener... ` and it works. – rchrd Mar 29 '21 at 10:19
6

You can add

  System.exit(0);

where you want the program to end, maybe immediately after the dispose() line.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • I thought that shut down the entire JVM. Will it only close for the current application? – jax Aug 25 '11 at 13:39
3

consider using JWindow(un-decoretad by defalut), but that's little bit complicating fact, that JWindow required initializations from JFrame (just must exist, nothing else) as parent

better would be add WindowListener and all Events/Actions would be redirected/managed this way

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

You know that the EXIT_ON_CLOSE field is also inherited by JDialog, right?

As mentioned by @camickr, EXIT_ON_CLOSE is not a valid value for the setDefaultCloseOperation method of the JDialog class. As stated by the API,

Sets the operation that will happen by default when the user initiates a "close" on this dialog. You must specify one of the following choices:

  • DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
  • HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the dialog after invoking any registered WindowListener objects.
  • DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the dialog after invoking any registered WindowListener objects.

If EXIT_ON_CLOSE is passed as an argument, an IllegalArgumentException will be thrown.

Community
  • 1
  • 1
mre
  • 43,520
  • 33
  • 120
  • 170
  • Yeah it does, I was using WindowBuilder for creating the GUI and it was not an available option so I thought it was not available. Thanks – jax Aug 25 '11 at 13:45
  • 1
    -1, Really? In JDK6, according to the API EXIT_ON_CLOSE is not valid and when I try using it I get an IllegalArgumentException at runtime. If this is a new feature in JDK7 I'll remove the down vote after a comment is added indicating the new functionality. – camickr Aug 25 '11 at 14:59
  • @mre not easy to do that programatically, all of Top-Level Containers has something **** call it wrong, http://stackoverflow.com/questions/5540354/java-swing-the-right-action-to-take-upon-closing-windows , maybe for refreshing another issue(s) http://stackoverflow.com/questions/6309407/remove-top-level-container-on-runtime – mKorbel Aug 25 '11 at 15:12
  • @camickr, Hmm..you're right. I still cannot find where this is written in the API though. I'm not sure that the down-vote was warranted, but that's the kind of person you are. So, whatever. If you could point me to the specific lines that state this field is not valid, I'd be more than appreciative. But this does not make my answer untrue. The API clearly shows that `EXIT_ON_CLOSE` is an inherited field. Not my fault this particular subtlety is poorly documented AFAIK. – mre Aug 26 '11 at 13:08
  • @mre, so its ok to upvote wrong answers, but its not ok to downvote wrong answers? What is the point of downvoting? This answer is wrong and misleading, that is why I down voted it! The valid values are clearly documented in the API for the `setDefaultCloseOperation()` method. I still can't believe this got an upvote AFTER my comment was made. By the way no where in my answer did I attack your character, I simply stated the answer was wrong. We all make mistakes (I know I do all the time), I just hope we learn from them. – camickr Aug 26 '11 at 14:24
  • @camickr, Not sure who upvoted, but thanks for pointing me in the right direction. And for others, see [this](http://download.oracle.com/javase/6/docs/api/javax/swing/JDialog.html#setDefaultCloseOperation%28int%29). – mre Aug 26 '11 at 14:27
0

For me worked only with windowClosing event:

dialog.addWindowListener(new WindowAdapter() 
                @Override
                public void windowClosing(WindowEvent e) {
                     System.exit(0);
                }
    });
Alessandro Mattiuzzi
  • 2,309
  • 2
  • 18
  • 24
0

you can try this below amazing source code -

JDialog dialog = (JDialog) container;
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModal(false);
dialog.setVisible(false);
dialog.dispose();

Runtime.getRuntime().exit(1);

this above said will shut off the process as well as after disposing the JDialog container, also one more benefit is there, if this JDialog is running above any other JFrame or JDialog, so the parent will not terminate but if this JDialog is running on its own, then the process will get terminated completely, Enjoy.

ArifMustafa
  • 4,617
  • 5
  • 40
  • 48