Search code examples
javaswingjframeapplication-close

show a new jframe on another jframe close event


I have 2 jframes(assume A and B) and when I close a one jframe(A) I need to show other jframe(B) I have a clue that I need to override defaultClosingOperation but I have no idea how to do that.any help would be appreciated .. thank you all.


Solution

  • You can add a Windows Listener to your frame.

    WindowListener myExitListener = new WindowAdapter() {
    
                @Override
                public void windowClosing(WindowEvent e) {
                    int confirmation = JOptionPane.showOptionDialog(jframe1, "Open frame2", "Open frame2", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
                    if (confirmation == 0) {
                      //open jframe2 here
                    }
                }
            };
    
    
    jframe1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    jframe1.addWindowListener(myExitListener);