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?