views:

840

answers:

5

i have three JFrames, i want to close one of them without influence to another. what can i do to get it ?

thanks Peter. I want to open two(or more) JFrames at the same time, and When I close one of them(use the default close button), others is still open.

+2  A: 

EDIT:
If I understand your edited question, then your application terminates when you close a JFrame even if there are others open.

If this is the case, make sure to use setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); on your JFrame when initializing, not setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  • DISPOSE_ON_CLOSE will terminate your application when the last JFrame is closed.
  • EXIT_ON_CLOSE will terminate your application as soon as that JFrame is closed.
  • HIDE_ON_CLOSE (default) will not terminate your application even if all JFrames are hidden.


You can use setVisible(false) on your JFrame if you want to display the same frame again.
Otherwise call dispose() to remove all of the native screen resources.

If that's not what you need, please edit your post to provide some more information about how and why you want to close the frame.

Peter Lang
thanks,but i close the window with the close-button at the upper-right corner. It doesn't use setVisible(false) definitely and must stop the thread.
Keating
I think i must override some mothed, i don't know which mothed but i believe it isn't the closing mothed.
Keating
I'm not sure what you try to achieve, could you please edit your question and add some information about what you try to do and what does not work?
Peter Lang
i edit the question, thanks
Keating
that's what i want ! thank you !
Keating
A: 

Does it help you ?

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TwoJFrames {
    public static void main(String[] args) {
     int nb = 4;
     if (args != null && args.length > 0) {
      nb = Integer.parseInt(args[0]);
     }

     final int frameCount = nb;
     SwingUtilities.invokeLater(new Runnable() {
      public void run() {
       for (int i = 0; i < frameCount; i++) {
        JFrame frame = new JFrame("Frame number " + i);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JPanel p = new JPanel(new BorderLayout());
        p.add(new JLabel("Click on the corner to close..."), BorderLayout.CENTER);
        frame.setContentPane(p);
        frame.setSize(200, 200);
        frame.setLocation(100 + 20 * i, 100 + 20 * i);
        frame.setVisible(true);
       }
      }
     });

    }
}
Laurent K
that's useful , thank you!
Keating
A: 

Many Many Thanx.. really helps

Kaka
Gnoupi
A: 

As someone completely new to Java and Swing, I had the same problem; I was opening a new JFrame with a button, and then when I closed it, both JFrames would disappear. This post helped me to realise I had confused classes with instances. When pushing the button, I was setting the class template of my new JFrame as visible, SecondFrame.setVisible(true); and trying to set DISPOSE_ON_CLOSE within the class itself

when in fact when pushing the button I should have made secondFrame secondframe = new secondFrame(); secondframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); secondframe.setVisible(true);

I'm happy that there are experienced people who post on boards like this...I was actually having trouble with this for ages

sqwest
Gnoupi
A: 

mean if I have two frame one in a other and I set to close second Jfrmae and when this second automat closed and First Jframe! if you understand someting :) please help! THN

vadeara