Kyle
Kyle

Reputation: 11

Switching between JPanels in different classes

I've stumbled upon this complications and have spent more than 4 hours debugging and googling but to no avail..

Basically what I have here is 1 JFrame, 2 JPanels. I had my JFrame setContentPane to 1 of the JPanel, and when I run the Application, the JFrame will appear with the JPanel inside.

Now this JPanel have 1 JButton inside it, when I click it I want it to switch to another JPanel. As you can see from the code, when I click the JButton(Add Product), I want the OnlineShopAdPane to switch to AddProduct. I tried using CardLayout but it only has NSEW formatting.

package OnlineShop.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OnlineShopMainFrame extends JFrame {

    /**
     * Launch the application.
     */
    AddProduct Add;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
                    MainFrame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public OnlineShopMainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);

        OnlineShopAdPane AdPanel = new OnlineShopAdPane();
        setContentPane(AdPanel);

    }


}


package OnlineShop.ui;

import javax.swing.JPanel;
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;

public class OnlineShopAdPane extends JPanel {

    /**
     * Create the panel.
     */

    public OnlineShopAdPane() {

        JLabel lblWhatDoYou = new JLabel("What do you want to do?");
        lblWhatDoYou.setBounds(28, 26, 160, 26);
        add(lblWhatDoYou);

        JButton btnAddProduct = new JButton("Add Product");
        btnAddProduct.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
            MainFrame.removeAll();
            MainFrame.add(new AddProduct());
            MainFrame.revalidate();
            MainFrame.repaint();
            }
        });

        btnAddProduct.setBounds(46, 75, 115, 23);
        add(btnAddProduct);

    }

}


package OnlineShop.ui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddProduct extends JPanel {
    private JTextField textField;

    /**
     * Create the panel.
     */
    public AddProduct() {

        JLabel lblProductName = new JLabel("Product Name:");
        lblProductName.setBounds(35, 26, 77, 24);
        add(lblProductName);

        JLabel lblProductDescription = new JLabel("Product Description:");
        lblProductDescription.setBounds(10, 50, 106, 24);
        add(lblProductDescription);

        textField = new JTextField();
        textField.setBounds(116, 28, 141, 20);
        add(textField);
        textField.setColumns(10);

        JTextArea textArea = new JTextArea();
        textArea.setBounds(116, 66, 141, 112);
        add(textArea);

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

            }
        });
        btnClose.setBounds(223, 244, 89, 23);
        add(btnClose);

    }

}

Upvotes: 0

Views: 1301

Answers (3)

camickr
camickr

Reputation: 324098

I tried using CardLayout but it only has NSEW formatting.

What does that mean? A CardLayout simply contains two or more panels. Only one panel is visible at a time. Each panel can use whatever layout it wants to layout the components on the panel.

when I click it I want it to switch to another JPanel.

That is exactly what CardLayout does. See the Swing tutorial on How to Use Card Layout for a working example and explanation.

Whenever I see code like remove/add/revalidate/repaint it should almost always be replaced with a CardLayout

Upvotes: 1

PrR3
PrR3

Reputation: 1246

The problem is pobably located at the following lines in class OnlineShopAdPane.java

OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
MainFrame.removeAll();
MainFrame.add(new AddProduct());
MainFrame.revalidate();
MainFrame.repaint();

your not referring to the frame where your JPanel is nested. instead your creating a new OnlineShopMainFrame

Upvotes: 0

nachokk
nachokk

Reputation: 14413

I think that with CardLayout you can resolve it, but another way is using for example a Handler to switch your panels.

    private JComponent container; // this could be your Frame
    private JComponent loadedComponent;

public void loadContent(JComponent component, Object object ) {

        if (loadedComponent != null) {
            loadedComponent.setVisible(false);
            container.remove(loadedComponent);
            loadedComponent = null;
        }
        //TODO may check layout
        container.add(component,object);
        component.setVisible(true);
        loadedComponent = component;
        container.validate();
    }

Upvotes: 0

Related Questions