user3779397
user3779397

Reputation: 11

Multiple JFrames in single java code

I have two JFrames in my java code, when I close one frame second frame is closed automatically, please tell how I can make them independent of each other?

My code is like this:

JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();

frame1.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame1.setUndecorated(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);

frame2.setSize(200,100);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);

Upvotes: 1

Views: 3256

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

Yours is a common Swing newbie error, and while yes, you could set the JFrame default close operation to DO_NOTHING_ON_CLOSE, you're then still stuck with a bad program design.

Instead, you should almost never display two JFrames at once as a JFrame is meant to display a main application window, and most applications, including and especially professional applications don't have multiple application windows. Instead, make the "mother" window a JFrame, and the child or dependent Window a JDialog, and your problem is solved in that when the child window closes, the application must remain open. The JDialog has the additional advantage of allowing it to be either modal or non-modal as the need dictates.

Other decent solutions including avoiding multiple windows altogether including use of JTabbedPane or the CardLayout to swap views.

Please read: The use of multiple JFrames, good bad practice


Edit
For example

import java.awt.Dialog.ModalityType;
import java.awt.Dimension;    
import javax.swing.*;

public class DialogEx {


   private static void createAndShowGui() {
      JFrame frame1 = new JFrame("DialogEx");
      frame1.setExtendedState(JFrame.MAXIMIZED_BOTH);
      frame1.setUndecorated(true);
      frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame1.setVisible(true);

      JDialog nonModalDialog = new JDialog(frame1, "Non-Modal Dialog", ModalityType.MODELESS);
      nonModalDialog.add(Box.createRigidArea(new Dimension(200, 200)));
      nonModalDialog.pack();
      nonModalDialog.setLocationByPlatform(true);
      nonModalDialog.setVisible(true);

      JDialog modalDialog = new JDialog(frame1, "Modal Dialog", ModalityType.APPLICATION_MODAL);
      modalDialog.add(Box.createRigidArea(new Dimension(200, 200)));
      modalDialog.pack();
      modalDialog.setLocationByPlatform(true);
      modalDialog.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Upvotes: 4

Abdul Fatir
Abdul Fatir

Reputation: 6357

This is because you set the default close operation of both the frames to EXIT_ON_CLOSE hence the program with exit irrespective of which frame's close button you clicked on. For your main frame you may set it to EXIT_ON_CLOSE and for other you may set it to DO_NOTHING_ON_CLOSE.

Also, as others have suggested use JDialogs instead of multiple frames.

Upvotes: 1

Related Questions