Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close one JFrame without closing another?

Tags:

java

swing

jframe

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?

like image 602
Keating Avatar asked Dec 22 '09 06:12

Keating


People also ask

How do I automatically close a JFrame?

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.

What is setDefaultCloseOperation in Java?

The default behavior is to simply hide the JFrame when the user closes the window. To change the default behavior, you invoke the method setDefaultCloseOperation(int) . To make the JFrame behave the same as a Frame instance, use setDefaultCloseOperation(WindowConstants. DO_NOTHING_ON_CLOSE) .

What is setDefaultCloseOperation JFrame Exit_on_close );?

Calling setDefaultCloseOperation(EXIT_ON_CLOSE) does exactly this. It causes the application to exit when the application receives a close window event from the operating system.

What does JFrame setVisible do?

The setSize(400,300) method of JFrame makes the rectangular area 400 pixels wide by 300 pixels high. The default size of a frame is 0 by 0 pixels. The setVisible(true) method makes the frame appear on the screen.


2 Answers

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.

like image 197
Peter Lang Avatar answered Sep 23 '22 23:09

Peter Lang


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);                 }             }         });      } } 
like image 37
Laurent K Avatar answered Sep 23 '22 23:09

Laurent K