1

I have 2 classes which extends Jframe. I would like to call the 2nd Jframe when button clicked and return back when close clicked. So on 1st JFrame I wrote this code

public void actionPerformed(ActionEvent e) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                    Lessons frameLessons = new Lessons();
                    frameLessons.setVisible(true);
                } catch (Exception e) {
                        e.printStackTrace();
                }
            }
        });
    }

But when I try to close the 2nd Jframe, the 1st also closes

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
M. Namiz
  • 99
  • 8
  • 2
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Oct 18 '15 at 06:26
  • *"I have 2 classes which extends Jframe."* This is something that IDEs typically encourage (extending GUI classes instead of just configuring an instance of the standard one) that should be avoided. Having said that, if determined to continue using the GUI builder, it would be better to extend `JPanel`. That way, all the panels can be used in a single top level container like frame - if that is appropriate, or shown in a dialog, or an applet, or.. *"call the 2nd Jframe when button clicked and return back when close clicked."* That suggests 2nd container should be a **modal `JDialog`** .. – Andrew Thompson Oct 18 '15 at 06:29

1 Answers1

-1

Try this:

Lessons frameLessons = new Lessons();
frameLessons.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frameLessons.setVisible(true);
Jad Chahine
  • 6,849
  • 8
  • 37
  • 59