53

I want to display two (or more) JFrames at the same time.
When I close one of them (use the default close button), the other frames should still be visible.

How can I do that?

Peter Lang
  • 54,264
  • 27
  • 148
  • 161
Keating
  • 733
  • 2
  • 7
  • 12

2 Answers2

90

If you do not want your application to terminate when a JFrame is closed, use

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

instead of

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

From the documentation:

  • DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
  • HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.
  • DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.
  • EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.

This was my answer before the question was clarified, might still be useful:

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.

Peter Lang
  • 54,264
  • 27
  • 148
  • 161
  • 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 Dec 22 '09 at 06:32
  • I think i must override some mothed, i don't know which mothed but i believe it isn't the closing mothed. – Keating Dec 22 '09 at 06:34
  • 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 Dec 22 '09 at 06:39
  • calling DISPOSE_ON_CLOSE before init() worked for me.. Thanks – codezoner Jan 11 '15 at 13:12
3

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
  • 3,503
  • 3
  • 30
  • 36
  • Dint worked for me . Then on design view from properties i have set defaultCloseOperation to Dispose and finally it work. – Kashinath Jun 07 '23 at 10:08