I am creating my first Java GUI application and I want to have multiple screen/panes. So far I have created two Java froms. One is called HomeInterface.form and the other is called RestockInterface.form.
However, I want to switch between both JPanels by clicking the buttons. The problem here is, I don't know how to implement this probably. At the moment everytime I click the Button backToHomeInterfaceButton
a new JFrame get created. Also, I have to create a new HomeInterface object in order to get the rootPanel. This is ridiculous.
Do you guys know how I can implement this probably without creating a new object and frame each time? I want to keep the same frame and just switch between the different rootPanels without creating a new one each time.
Please keep in mind, I don't want to use CardLayout or TabbedLayout, I am using the IntelliJ GridLayout and want to keep this!
HomeInterface.form code:
public class HomeInferface{
private JPanel rootPanel;
private JButton thisIsMyButtonButton;
private JPanel test;
public HomeInferface(){
JFrame frame = new JFrame("Beispiel Frame");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(rootPanel);
frame.setVisible(true);
thisIsMyButtonButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SellInterface sellInterface = new SellInterface();
frame.setContentPane(sellInterface.getRootPanel());
frame.revalidate();
}
});
public JPanel getRootPanel() {
return rootPanel;
}
}
}
RestockForm.form:
public class RestockInterface {
private JPanel rootPanel;
private JButton backToHomeInterfaceButton;
public RestockInterface(JFrame frame) {
backToHomeInterfaceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
HomeInferface homeInterface = new HomeInferface();
frame.setContentPane(homeInterface.getRootPanel());
frame.revalidate();
}
});
}
public JPanel getRootPanel() {
return rootPanel;
}
}