-1

I want to display three JFrame objects at the same time. When I close one of them from the up corner, the other frames should still be visible but they're not! I searched for it and I just got that the problem is related to these:

  • setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

How can I do that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) But having said that, `DISPOSE_ON_CLOSE` should 'wrok' for this. See [this answer](http://stackoverflow.com/a/7143398/418556) to see it work. – Andrew Thompson Sep 06 '14 at 12:47
  • Can you show what are you trying to do? – joey rohan Sep 06 '14 at 12:49
  • What went wrong when you tried `setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);` ? I can't see why it wouldn't work. – Dawood ibn Kareem Sep 06 '14 at 12:52
  • there is a frame ,clicking a button opens an other frame . when i close one of these frames from the close icon at the corner of the screen , both of frames will close . but i just want one of them closed . – Mahsa Chenari Sep 06 '14 at 12:54
  • the point is i don't know where to type setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); – Mahsa Chenari Sep 06 '14 at 12:56
  • Oh, OK. Well, I'm assuming you've got a class that extends `JFrame`, and gives it the features that you want. You could write that line of code somewhere in the constructor for that class. – Dawood ibn Kareem Sep 06 '14 at 19:04

1 Answers1

2

Are you extending JFrame? Don't.

Also when you inherit JFrame, the CLOSE operation gets defined for every window you create, so it will cause termination of window.

I din't had any issues with the following code :

 JFrame j1=new JFrame();
 JFrame j2=new JFrame();
 JFrame j3=new JFrame();

 j1.setVisible(true);
 j2.setVisible(true);
 j3.setVisible(true);

 j1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 j2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 j3.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

EDIT:

"Where should i type this"

Where ever it suits you.What I can think(from your closing problem) you are having as structure something like :

public class FooClass extends JFrame {

   FooClass()
    {

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String args[]) {

        new FooClass().setVisible(true);
        new FooClass().setVisible(true);
        new FooClass().setVisible(true);

    }
}

Don't force your class to extend JFrame, rather create the object as shown above And use '.' operator to preform functions JSomeObject.SetThis...();

EDIT 2:

Or just see if setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE) helps you.

OR

You can add a window listener and call setVisible(false) on JFrame :

 addWindowListener(new WindowAdapter() {
    @Override
  public void windowClosing(WindowEvent evt) {
    setVisible(false);
  }
});
joey rohan
  • 3,505
  • 5
  • 33
  • 70