How do you add a JPanel from another class to a panel with CardLayout?

342 Views Asked by At

I'm working on a group project and I'm the one making the GUI figuring it'd be good to practice with it. The program is supposed to be a pizza ordering system (pretty standard stuff) and what I'm trying to accomplish is that I have a main class that creates an application window. Inside this window is a panel that uses CardLayout with a button that when pressed calls another JPanel from another class dedicated specifically to that panel and places it as a card in the layout to be swapped back and forth from as normal.

What I have so far are the different panels I wish to call and the main class which has the window and main card panel. I can have it swap easily between panels created within the main class but when I try to use the panels from the other classes it just swaps to a blank panel when it should show the other class's panel.

The main class

package PizzaGUI;

import java.awt.EventQueue;
import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Font;

public class PizzaSystem {

    private JFrame frame;
    Toppings toppingsPanel;
    MainMenu menuPanel;
    JButton loginBtn;
    JPanel mainPanel;
    CardLayout cl;
    

    //MAIN
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    PizzaSystem window = new PizzaSystem();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //Constructor
    public PizzaSystem() {
        initialize();
    }
    
    //Initialize the GUI
    private void initialize() {
        
        cl = new CardLayout();
        
        frame = new JFrame();
        frame.setBounds(100, 100, 893, 527);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        mainPanel = new JPanel();
        mainPanel.setBounds(10, 10, 859, 470);
        frame.getContentPane().add(mainPanel);
        mainPanel.setLayout(cl);
        
        JPanel panel_2 = new JPanel();
        mainPanel.add(panel_2, "test");
        panel_2.setLayout(null);
        
        JLabel lblNewLabel = new JLabel("It Worked");
        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 45));
        lblNewLabel.setBounds(282, 118, 312, 103);
        panel_2.add(lblNewLabel);
        
        JPanel panel_1 = new JPanel();
        
        loginBtn = new JButton();
        loginBtn.setText("Login");
        loginBtn.setBounds(175, 72, 199, 154);
        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showCard("menu");
            }
        });
        
        panel_1.add(loginBtn);
        
        mainPanel.add(panel_1, "loginPanel");
        JPanel menuPanel = new MainMenu();
        mainPanel.add(menuPanel, "menu");
        
        showCard("loginPanel");
        
    }
    
    //Call card matching the key
    public void showCard(String key) {
        cl.show(mainPanel, key);
    }
}

and one of the classes with the panel (format is messed up but should work still)

package PizzaGUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

public class MainMenu extends JPanel {

    public MainMenu() {
        
        JPanel panel1 = new JPanel();
        panel1.setBounds(100, 100, 893, 572);
        panel1.setBackground(Color.PINK);
        panel1.setLayout(null);
        panel1.setVisible(true);
        
        JLabel logoLabel = new JLabel("");
        logoLabel.setBounds(10, 10, 100, 110);
        panel1.add(logoLabel);
        ImageIcon image1 = new ImageIcon("C:\\Users\\thera\\eclipse-workspace\\PizzaSystem\\Images\\MamaJane1.png");
        logoLabel.setIcon(new ImageIcon("C:\\Users\\thera\\eclipse-workspace\\PizzaSystem\\Images\\MamaJane.png"));
        
        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setBounds(133, 113, 548, 402);
        tabbedPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        tabbedPane.setBackground(Color.PINK);
        tabbedPane.setForeground(Color.GRAY);
        tabbedPane.setFont(new Font("Tahoma", Font.PLAIN, 20));
        tabbedPane.setToolTipText("");
        panel1.add(tabbedPane);
        
        JPanel panel = new JPanel();
        tabbedPane.addTab("MENU", null, panel, null);
        panel.setLayout(null);
        
        
        //*******************************************************************************************************
        //Pepperoni menu section
        JLabel pizza1Image = new JLabel("IMAGE");
        pizza1Image.setBounds(6, 25, 100, 100);
        panel.add(pizza1Image);
        ImageIcon image2 = new ImageIcon("C:\\Users\\thera\\eclipse-workspace\\PizzaSystem\\Images\\Pepperoni.jpg");
        Image pizza1 = image2.getImage();
        Image pepperoni = pizza1.getScaledInstance(100, 100, java.awt.Image.SCALE_SMOOTH);
        image2 = new ImageIcon(pepperoni);
        pizza1Image.setIcon(image2);
        
        JLabel pepperoniLabel = new JLabel("PEPPERONI");
        pepperoniLabel.setHorizontalAlignment(SwingConstants.CENTER);
        pepperoniLabel.setBounds(110, 25, 86, 48);
        pepperoniLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(pepperoniLabel);
        
        
        JButton pepperoniOrderBtn = new JButton("ORDER");
        pepperoniOrderBtn.setBounds(110, 79, 86, 47);
        panel.add(pepperoniOrderBtn);
        pepperoniOrderBtn.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
            }
        });
        //Pepperoni End
        
        JButton accountButton = new JButton("Account");
        accountButton.setBounds(10, 414, 113, 39);
        accountButton.setBackground(Color.WHITE);
        panel.add(accountButton);
        
        JButton checkoutButton = new JButton("Checkout");
        checkoutButton.setBounds(713, 438, 138, 31);
        panel.add(checkoutButton);
        
        JButton logoutButton = new JButton("Logout");
        logoutButton.setBounds(10, 463, 113, 52);
        panel.add(logoutButton);
        
        JTextArea txtrOrderInfoGoes = new JTextArea();
        txtrOrderInfoGoes.setBounds(703, 10, 154, 418);
        txtrOrderInfoGoes.setText("Order Info\r\nGoes Here\r\n\r\nPepperoni Pizza x1\r\n$6.50");
        panel.add(txtrOrderInfoGoes);
        
        JButton clearOrderButton = new JButton("Clear");
        clearOrderButton.setBounds(723, 479, 113, 36);
        panel.add(clearOrderButton);
        
        JLabel titleLabel = new JLabel("MAMA JANE'S PIZZERIA");
        titleLabel.setBounds(147, 10, 534, 65);
        panel.add(titleLabel);
        titleLabel.setFont(new Font(titleLabel.getFont().getName(), Font.PLAIN, 50));
        
        JLabel storeInfoLabel = new JLabel("<html>1234 Fakelane Rd <br> 10001, Faketopia, USA <br> 111-11-PIZZA <br> Mon-Fri 11AM to 10pm <br> Sat-Sun 10AM to 11pm");
        storeInfoLabel.setBounds(10, 130, 113, 274);
        panel.add(storeInfoLabel);
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(406, 85, 2, 2);
        panel.add(scrollPane);
        logoutButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

}


I can't tell where I've gone wrong and I've spent roughly the last three hours trying to fix this and searching the internet for answers to no avail so thank you in advance if you can help me out.

Also I apologize in advance, I know I end up misusing the proper terminology for programming alot, I understand what things are just forget what to properly call them sometimes.

1

There are 1 best solutions below

1
MadProgrammer On

So, basically, I took out all the null layouts and "manual" layout code, as it's just going to mess with you to no end AND added add(panel1); to the end of the MainMenu constructor - as, I've said, NOTHING was added to MainMenu, so, nothing was going to get displayed.

Screen one

Screen Two

Before you tell me that "this isn't the layout I want", understand that I understand that, but my point is, null layouts are a really bad idea, as almost the entire Swing API relies the layout managers in one way or another.

I appreciate that layout management can seem like a complex subject, but it solves some very complex problems and it's worth taking the time to learn them. Remember, you're not stuck to a single layout manager, you can use component components to adjust individual containers to their individual needs.

You can take a look at:

to some ideas how you might approach designing a complex UI.

You should also take a look at Laying Out Components Within a Container

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

public class PizzaSystem {

    private JFrame frame;
//    Toppings toppingsPanel;
    MainMenu menuPanel;
    JButton loginBtn;
    JPanel mainPanel;
    CardLayout cl;

    //MAIN
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    PizzaSystem window = new PizzaSystem();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //Constructor
    public PizzaSystem() {
        initialize();
    }

    //Initialize the GUI
    private void initialize() {

        cl = new CardLayout();

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainPanel = new JPanel(cl);
        frame.getContentPane().add(mainPanel);

        JPanel panel_1 = new JPanel(new GridBagLayout());

        loginBtn = new JButton();
        loginBtn.setText("Login");
        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showCard("menu");
            }
        });

        panel_1.add(loginBtn);

        mainPanel.add(panel_1, "loginPanel");

        JPanel menuPanel = new MainMenu();
        mainPanel.add(menuPanel, "menu");

        frame.pack();

        showCard("loginPanel");

    }

    //Call card matching the key
    public void showCard(String key) {
        cl.show(mainPanel, key);
    }

    public class MainMenu extends JPanel {

        public MainMenu() {

            JPanel panel1 = new JPanel(new BorderLayout());

            JLabel logoLabel = new JLabel("Logo");
            panel1.add(logoLabel, BorderLayout.NORTH);

            JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
            tabbedPane.setToolTipText("");
            panel1.add(tabbedPane, BorderLayout.CENTER);

            JPanel panel = new JPanel();
            tabbedPane.addTab("MENU", null, panel, null);

            //*******************************************************************************************************
            //Pepperoni menu section
            JLabel pizza1Image = new JLabel("IMAGE");
            panel.add(pizza1Image);

            JLabel pepperoniLabel = new JLabel("PEPPERONI");
            pepperoniLabel.setHorizontalAlignment(SwingConstants.CENTER);
            panel.add(pepperoniLabel);

            JButton pepperoniOrderBtn = new JButton("ORDER");
            pepperoniOrderBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                }
            });
            //Pepperoni End

            JButton accountButton = new JButton("Account");
            panel.add(accountButton);

            JButton checkoutButton = new JButton("Checkout");
            panel.add(checkoutButton);

            JButton logoutButton = new JButton("Logout");
            panel.add(logoutButton);

            JTextArea txtrOrderInfoGoes = new JTextArea(10, 20);
            txtrOrderInfoGoes.setText("Order Info\r\nGoes Here\r\n\r\nPepperoni Pizza x1\r\n$6.50");
            panel.add(txtrOrderInfoGoes);

            JButton clearOrderButton = new JButton("Clear");
            panel.add(clearOrderButton);

            JLabel titleLabel = new JLabel("MAMA JANE'S PIZZERIA");
            panel.add(titleLabel);
            titleLabel.setFont(new Font(titleLabel.getFont().getName(), Font.PLAIN, 50));

            JLabel storeInfoLabel = new JLabel("<html>1234 Fakelane Rd <br> 10001, Faketopia, USA <br> 111-11-PIZZA <br> Mon-Fri 11AM to 10pm <br> Sat-Sun 10AM to 11pm");
            panel.add(storeInfoLabel);

            JScrollPane scrollPane = new JScrollPane();
            panel.add(scrollPane);
            logoutButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });

            add(panel1);
        }

    }
}