-1
public ButtonGrid(int width, int length){ //constructor
            frame.setTitle("MPC");
            frame.setLayout(new GridLayout(width,length)); //set layout
            grid=new JButton[width-1][length]; //allocate the size of grid
            for(int y=0; y<length; y++){
                    for(int x=0; x<width-1; x++){
                            grid[x][y]=new JButton("("+x+","+y+")"); //creates new button     
                            frame.add(grid[x][y]); //adds button to grid
                            grid[x][y].addActionListener(this);
                            grid[x][y].addKeyListener(this);
                            //grid[x][y].setMnemonic(KeyEvent.VK_0);
                            grid[x][y].setPreferredSize(new Dimension(75,75));
                    }
            }

            for(int i =0; i<boxList.length; i++)
                box.addItem(boxList[i]);
            box.addActionListener(this);
            frame.add(box);
            frame.add(met_speed);
            frame.add(Play);
            Play.addActionListener(this);


            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack(); //sets appropriate size for frame
            frame.setVisible(true); //makes frame visible



    }
    public void newWindow(){
        JFrame frame1 = new JFrame();

        frame1.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        JTextField[] btn = new JTextField[12];
        JLabel[] lbl = new JLabel[12];
        JLabel name = new JLabel("Preset Name");
        JTextField name1 = new JTextField();
        name1.setPreferredSize(new Dimension(100,25));


        gbc.gridx = 0;
        gbc.gridy = 0;
        frame1.add(name, gbc);
        gbc.gridx++;
        frame1.add(name1, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        for(int i = 0; i<12; i++){
            lbl[i] = new JLabel("Button" + (i+1));
            frame1.add(lbl[i], gbc);
            gbc.gridy++;
        }

        gbc.gridx = 1;
        gbc.gridy = 1;
        for(int i = 0; i<12; i++){
            btn[i] = new JTextField();
            btn[i].setPreferredSize(new Dimension(75,25));
            frame1.add(btn[i], gbc);
            gbc.gridy++;
        }


        gbc.gridx = 0;
        gbc.gridy = 14;
        JButton save = new JButton("Save");
        frame1.add(save, gbc);

        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.pack(); //sets appropriate size for frame
        frame1.setVisible(true); //makes frame visible

    }

The first function ButtonGrid is called in the main and contains the real program. After a button is pressed, the newWindow() is called as a popup. Up to that point, it works fine, but when I close frame1, it closes frame with it and ends the program.

Am I doing this correctly or is there something I need to add?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
user3562657
  • 167
  • 3
  • 15
  • 1
    You have `frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` which exactly means "Exit the program when I close `frame1`." – khelwood Dec 02 '14 at 00:57
  • 2
    Have a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) for reasons why you should avoid showing multiple frames... – MadProgrammer Dec 02 '14 at 00:58
  • `grid[x][y].setPreferredSize(new Dimension(75,75));` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Dec 02 '14 at 01:11

2 Answers2

5

Best solution: don't "pop-up" a JFrame. Use a JDialog instead since closing it won't close your program, and this is exactly what they're made for. And yes, you can change the JFrame's defaultCloseOperation to DO_NOTHING_ON_CLOSE, but you'd still be using a main program window for something it was not meant to do. Better to use the correct tool for the job, the JDialog. Note also that a JDialog can take whatever complex GUI you're currently putting in your "pop-up" JFrame.

For example:

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

public class TwoWindows {
   public static final int PREF_W = 600;
   public static final int PREF_H = 400;

   private static void createAndShowGui() {
      final JFrame frame = new JFrame("Main Application");
      JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));

      buttonPanel.add(new JButton(new AbstractAction("New JFrame") {
        {
           putValue(MNEMONIC_KEY, KeyEvent.VK_F);
        }

         @Override
         public void actionPerformed(ActionEvent e) {
            JFrame frame2 = new JFrame("Frame 2");
            frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setupAndDisplay(frame2);
         }
      }));
      buttonPanel.add(new JButton(new AbstractAction("New Modeless JDialog") {
        {
           putValue(MNEMONIC_KEY, KeyEvent.VK_D);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
           JDialog dialog = new JDialog(frame, "Modeless Dialog", ModalityType.MODELESS);
           setupAndDisplay(dialog);
        }
     }));

      buttonPanel.add(new JButton(new AbstractAction("New Modal JDialog") {
        {
           putValue(MNEMONIC_KEY, KeyEvent.VK_M);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
           JDialog dialog = new JDialog(frame, "Modal Dialog", ModalityType.APPLICATION_MODAL);
           setupAndDisplay(dialog);
        }
     }));

      JPanel mainPanel = new JPanel() {
         @Override
         public Dimension getPreferredSize() {
            if (isPreferredSizeSet()) {
               return super.getPreferredSize();
            }
            return new Dimension(PREF_W, PREF_H);
         }
      };

      mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      mainPanel.add(buttonPanel);

      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   private static void setupAndDisplay(Window window) {
      window.add(Box.createRigidArea(new Dimension(200, 100)));
      window.pack();
      window.setLocationByPlatform(true);
      window.setVisible(true);
   }

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

Please run this to note the difference in behaviors between the two types of top level windows.:

  • When a second JFrame is created, a second and unncessary icon is created on the toolbar since the OS thinks that you're creating a completely new application rather than a 2nd window in a single application. The JDialog does not do this.
  • The second JFrame can go behind the main JFrame window while the JDialog, since it is a sub-window of the main application always remains on top of the main window.
  • If you close the main window when the dialog is visible, the entire application appropriately closes, but not so the 2nd JFrame, meaning this can leave you with a dangling window that does not have the main application to support it.
  • The JDialog has the option of being displayed in a modal fashion, meaning the underlying window is locked from user interaction until the dialog is no longer visible. This option is not available with a JFrame.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2

You may try this:

frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//<-- destroy only this frame
  • plus one, see also [this answer](http://stackoverflow.com/a/7143398/418556) for a demo. But ultimately, see the already mentioned.. [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Dec 02 '14 at 01:10
  • For now I'm going to use this one, but will look into @MadProgrammer 's post. Thank you though – user3562657 Dec 02 '14 at 01:24
  • @user3562657: why are you using this and not a JDialog? Do you really want to create new unnecessary icons on the user's toolbar? Do you really want to have issues over making sure which window remains over the other? Again, this is not the best solution and should not be the accepted answer. – Hovercraft Full Of Eels Dec 02 '14 at 01:47