I have a two JPanels, one that is displayed as a home/welcome page, the other displayed when a user clicks a button. The first panel does not disappear when the button is clicked, the second panel sort of displays its components at the same time so there is two panels worth of buttons/text fields etc both visible at the same time.
How do I fix this so panel1 disappears/panel2 appears?
(If I set the container visibility to false after button click, neither panel's components are displayed.)
public class mainApplication {
private static JFrame mainApp;
private static JPanel panel1;
private static JPanel panel2;
public mainApplication() {
JFrame.setDefaultLookAndFeelDecorated(true);
mainApp = new JFrame("Keystroke Authenticator Application");
mainApp.setSize(640, 480);
mainApp.setLocationRelativeTo(null);
mainApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainApp.add(panel1());
mainApp.setVisible(true);
}
private JPanel panel1() {
panel1 = new JPanel();
panel1.setSize(640,480);
Container contain1 = mainApp.getContentPane();
//Buttons, text fields and labels are configured with groupLayout here
panel1.setVisible(true);
buttonNew.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent clickNew) {
panel2 = panel2();
panel1.setVisible(false);
//contain1.setVisible(false); - neither panel are displayed
}
}
);
return panel1;
}
private JPanel panel2() {
panel2 = new JPanel();
panel2.setSize(640,480);
Container contain2 = mainApp.getContentPane();
//Buttons, text fields and labels are configured with groupLayout here
panel2.setVisible(true);
mainApp.add(panel2);
}
}
CodePudding user response:
I solved my own problem, it seemed to be the fact I was creating a container within each JPanel and using it with the GroupLayout. I removed the created container and replaced the container with the name of the JPanel:
//working code
GroupLayout layout = new GroupLayout(panel1);
panel1.setLayout(layout);
//instead of the original below
GroupLayout layout = new GroupLayout(container1);
container1.setLayout(layout);
CodePudding user response:
I would recommend using a layout manager; this should solve most of your problems.
public mainApplication() {
//normal formatting stuff
mainApp.setLayout(new FlowLayout()); //This will make things appear/disappear
mainApp.setResizable(false) //This will stop your frame from changing sizes on you
}
private JPanel panel1(JFrame frame) {
//normal formatting stuff
frame.add(panel1); //this will make your panel appear in the frame
//more formatting stuff and button creation
buttonNew.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel2 = panel2();
frame.remove(panel1);
frame.add(panel2);
frame.pack(); //this swaps out your components so that the frame displays panel2 instead. Pack makes it repaint itself.
}
return panel1;
}
Basically, what you were doing was telling the frame to paint the first panel, but then you told it to paint over it with panel 2, and never said to stop painting panel 1. Using a layout manager handles all of this behind the scenes and helps for other stuff in the long run.