1

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;
    }
}
Jan
  • 1,180
  • 3
  • 23
  • 60
  • 4
    This, `"Please keep in mind, I don't want to use CardLayout ....., I am using the IntelliJ GridLayout and want to keep this!"` really doesn't make sense as you know that you can nest JPanels, each using its own layout, and so using one layout does not preclude using another. The canonical solution is and remains: use a CardLayout. Period. – DontKnowMuchBut Getting Better Dec 01 '18 at 16:49
  • Agree -- the above is your solution – Hovercraft Full Of Eels Dec 01 '18 at 16:50
  • `I don't want to use CardLayout` - the parent panel uses the CardLayout. The individuals panels you add to the panel using CardLayout can use whatever layout you want. – camickr Dec 01 '18 at 17:41
  • Alright, thank you guys. However, I still don’t know how to implement tho probably... could you give me some tips? – Jan Dec 01 '18 at 18:14
  • You've gotten all the tips you need -- remember to **nest** JPanels, add one (or more) inside of another, each using its own layout – Hovercraft Full Of Eels Dec 01 '18 at 18:16
  • @Jan check out this [demo project](https://www.dropbox.com/s/719it8encpj8leh/CardDemo.zip?dl=0) for IntelliJ IDEA. Does it help? – CrazyCoder Dec 01 '18 at 22:25

0 Answers0