Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Accessing JPanel components from a seperate class

So I want to have a CardLayout class that switches between a menu page and a main app page, but I want to design those two panels in their own classes, then add an ActionListener and a CardLayout in a different class, and have the ActionListener use a button created in one of the panel classes.

Here is a (Not so short) SSCCE that kind of covers what I’m trying to say:

import java.awt.*;
import javax.swing.*;

public class MenuPanel extends Frame{
    JPanel menuPanel;
    JButton login;
    JButton signup;

    public MenuPanel(){
        menuPanel = new JPanel(new GridBagLayout());
        login = new JButton("Login");
        signup = new JButton("Signup");
        
        menuPanel.add(login);
        menuPanel.add(signup);
        
    }
}
import java.awt.*;
import javax.swing.*;

public class MainPanel extends JFrame{
    JPanel menuPanel;
    JButton login;
    JButton signup;

    public MainPanel(){
        mainPanel = new JPanel(new GridBagLayout());=
        
        menuPanel.setBackground(Color.grey);
        
    }
}
import java.awt.*;
import javax.swing.*;

public class CardLayout extends Frame implements ActionL {
//Now how do I add the frames from the other classes so that I can add them to my CardLayout?
CardLayout cl = new CardLayout();
JPanel panelCont;
    public CardLayout() {
        frame.add(panelCont);

        panelCont = new JPanel(cl);
        //Here is where I'm having trouble
        panelCont.add(menuPanel, "1");
        panelCont.add(mainPanel, "2");

        cl.show(panelCont, "1");

        login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                cl.show(panelCont, "2");
            }
        });
    }
}

public class Main {
    public static void main(String[] args) {
        new CardLayout();
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You cannot add Frames to another component. Frame is a top level component with a native peer. You should subclass from something else (JPanel?) instead

Also, btw what you are doing is not good design. Generally, in MVC Swing design, all view and control aspects should be in one class. Don’t split the view into multiple classes unless each of those classes stands up as its own reusable widget

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading