4

I have a JFrame which on instantiation calls a custom JDialog(Login). If login is successful i want that jFrame to be visible. And if user presses escape/cancel on that login dialog the whole application should be closed.

How can i do so...

Currently if i dispose dialog the jFrame gets visible.

Shashank Degloorkar
  • 3,041
  • 4
  • 31
  • 48

2 Answers2

5

Assuming you have access to your JFrame via a frame variable, you can simply call:

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));

It is probably better than calling a System.exit() as it will enable you to run some cleanup code if you have registered a window closing listener to your frame.

assylias
  • 310,138
  • 72
  • 642
  • 762
3

You can override the dialog close event :

dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

dialog.addWindowListener(new WindowAdapter() {

  public void windowClosing(WindowEvent we) {  
    //Release you source, close all your frames or call a brutal System.exit(0);
  }
});
alain.janinm
  • 19,595
  • 10
  • 61
  • 107