Search code examples
javaswingoopcardlayout

how to show panels of other classes in cardlayout?


i have created this simple program . my problem is that i want to display panels of other classes without opening new JFrame . but i could not create any solution. can some one make simple code to show how to access Panels of other classes and replace current panel ?

package cardlayout;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Clayou {

    private JFrame jf = new JFrame("card layout");
    private JPanel jp = new JPanel();
    private JPanel jp1 = new JPanel();
    private JPanel jp2 = new JPanel();
    private JButton jb1 = new JButton("second");
    private JButton jb2 = new JButton("first");
    private CardLayout cl = new CardLayout();

    Clayou() {

        jp.setLayout(cl);
        jp1.add(jb1);
        jp2.add(jb2);

        jp1.setBackground(Color.black);
        jp2.setBackground(Color.white);

        jp.add(jp1, "1");
        jp.add(jp2, "2");

        cl.show(jp, "1");

        jb1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(jp, "2");

            }
        });
        jb2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(jp, "1");

            }
        });
        jf.add(jp);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Clayou();
            }
        });
    }
}

Solution

  • I just answered a question which might as well have the same answer as this. Changing panels in gui I think this will also help you too. There is class Card which you extend to allow for more card types. I have made two card types for this example.

    imports

    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import java.awt.*;
    import java.util.Vector;
    

    main class

    public class Gui extends JFrame implements ListSelectionListener {
    
        Vector<String> menuList = new Vector<>();
        JList<String> menu = new JList<>(menuList);
    
        JPanel content = new JPanel(new CardLayout());
    
        Gui(){
            //put menu on left, content in middle
            add(menu, BorderLayout.WEST);
            add(content, BorderLayout.CENTER);
            menu.addListSelectionListener(this);
    
            //add multiple cards
            addCard(new SimpleLabelCard("First Item", "First Item Text"));
            addCard(new SimpleLabelCard("Second Item", "Second Item Text"));
            addCard(new SimpleTextAreaCard("Third Item", "Third Item Text"));
    
    
            //set content to first item
            ((CardLayout) content.getLayout()).show(content, "First Item");
        }
    
        private void addCard(Card c){
            menuList.add(c.name);
            content.add(c, c.name);
        }
    
    
        public static void main(String [] args){
            Gui gui = new Gui();
            gui.setSize(600, 500);
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setVisible(true);
        }
    
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if(e.getValueIsAdjusting()) return;
    
            //set card layout from JList menu
            ((CardLayout) content.getLayout()).show(content, menu.getSelectedValue());
        }
    }
    

    other classes, put each one in their respective file.

    //Card.java
    //this is the basic card class the extends JPanel
    //it contains the name so you can easily switch to it
    public class Card extends JPanel{
        public final String name;
    
        public Card(String name){
            this.name = name;
        }
    }
    
    //SimpleLabelCard.java
    //extends Card, so it is also a JPanel
    public class SimpleLabelCard extends Card{
    
        private JLabel label = new JLabel();
    
        public SimpleLabelCard(String name, String text) {
            super(name);
            label.setText(text);
            add(label);
        }
    }
    
    //SimpleTextAreaCard.java
    public class SimpleTextAreaCard extends Card{
    
        private JTextArea text = new JTextArea();
    
        public SimpleTextAreaCard(String name, String text) {
            super(name);
            this.text.setText(text);
            setLayout(new BorderLayout());
            add(this.text, BorderLayout.CENTER);
        }
    }
    

    now if you want to make another type of card just extend the card class. I hope this is straight forward enough. It was actually a learning experience for me.