tags:

views:

200

answers:

4

Hello guys, Say If i have two classes, in each class is a different JFrame, e.g JFrame A and JFrame B ( in seperate classes).

Now from the constructor of JFrame A I may push a button with an actionlistener attached, which will instantiate the other class, thus creating JFrame B. The problem is when JFrame B is created, both the JFrames are visible. If i close JFrame B, then JFrame A closes as well. How can i make it so only JFrame B closes?

Thanks

edit DISPOSE_ON_CLOSE does not work for me, it closes all the jframes.

some sample code:

public class classone {
  public classone() {
    JFrame a = new JFrame("this is A");
    classtwo newFrame = new classtwo(); 
  }
}

public class classtwo {
  public classtwo() {
    Jframe b = new JFrame("this is B");
    b.setDefaultCloseOperation(b.DISPOSE_ON_EXIT);
  }
}

please ignore any syntax errors, just for demonstration.

+1  A: 

For the JFrame B, set the default close operation to "dispose" as shown below :

frameB.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Then closing the child windows won't shut down your entire application.

HTH ! ;-)

missingfaktor
hi there, i tried that, it still seems to close the all the jframes!
KP65
@Keval : It would be nice if you could post the relevant parts of code here.
missingfaktor
posted an example up now, thanks
KP65
@Keval : Make sure you're calling `setDefaultCloseOperation` before making the JFrame visible. Or else, it might not take effect. Check it and then report back.
missingfaktor
+1  A: 

Do you have DISPOSE_ON_CLOSE on one frame and EXIT_ON_CLOSE on the other? If so then that would explain why your program is exiting prematurely. Ensure that all frames are set to DISPOSE_ON_CLOSE.

finnw
A: 

Just DO_NOTHING_ON_CLOSE and addWindowListener in WindowClosing method show a JOptionPane.showConfirmDia and if result return no(1) then return; else system.exit(0); its all

Davut Gürbüz
A: 

**

I got question now. Just when you create an instance of a Window tell how this object live, Review this code
               ...
        new JFrame(){
            @Override
            public synchronized void addWindowListener(WindowListener l) {
                // You may ask here also add windowClosing method and look at my previous post
                super.addWindowListener(l);
            }
        }.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ....

**

Davut Gürbüz