Closing a java program properly when JDialog is the main window
Asked Answered
D

6

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?

Decalcify answered 25/8, 2011 at 13:36 Comment(0)
F
6

You can add

  System.exit(0);

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

Fong answered 25/8, 2011 at 13:38 Comment(1)
I thought that shut down the entire JVM. Will it only close for the current application?Decalcify
D
20

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.

Derbent answered 25/8, 2011 at 13:43 Comment(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.Russon
F
6

You can add

  System.exit(0);

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

Fong answered 25/8, 2011 at 13:38 Comment(1)
I thought that shut down the entire JVM. Will it only close for the current application?Decalcify
S
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

Sweatband answered 25/8, 2011 at 13:51 Comment(0)
N
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.

Norine answered 25/8, 2011 at 13:42 Comment(6)
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. ThanksDecalcify
-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.Vulcanism
@Norine not easy to do that programatically, all of Top-Level Containers has something **** call it wrong, #5540854 , maybe for refreshing another issue(s) #6309907Sweatband
@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.Norine
@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.Vulcanism
@camickr, Not sure who upvoted, but thanks for pointing me in the right direction. And for others, see this.Norine
D
0

For me worked only with windowClosing event:

dialog.addWindowListener(new WindowAdapter() 
                @Override
                public void windowClosing(WindowEvent e) {
                     System.exit(0);
                }
    });
Disenchant answered 7/9, 2017 at 7:40 Comment(0)
D
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.

Dinnie answered 12/12, 2017 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.