开发者

Closing a java program properly when JDialog is the main window

开发者 https://www.devze.com 2023-03-30 22:30 出处:网络
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).

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 a开发者_Go百科round in the background

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

How can I close my application properly?


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.


You can add

  System.exit(0);

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


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


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.


For me worked only with windowClosing event:

dialog.addWindowListener(new WindowAdapter() 
                @Override
                public void windowClosing(WindowEvent e) {
                     System.exit(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.

0

精彩评论

暂无评论...
验证码 换一张
取 消