Search code examples
javaswingclassappletcardlayout

Card Layout with different classes


I need help with an applet that I am making for my coursework.

I am using card layout and I want the content of each card to be taken from different classes. Then in each card I want buttons to switch to other cards. I've seen examples that work in a similar way but they are based on JFrame rather than applet.

Please do not send me any external links, I've been researching card layout for several days already. I am a Java beginner and I am a slow learner. I would like someone to show me a basic example and explain how it is done. Thank you!

public class MainClass extends JApplet implements ActionListener {

        boolean inAnApplet = true;
        JButton btn1, btn2;
        JPanel cards;
        final static String PANEL1 = "";
        final static String PANEL2 = "";


        public MainClass() {

            Container contentPane = getContentPane();
            JPanel cbp = new JPanel();        
            cbp.add(new JLabel ("Hello world!"));

            contentPane.add(cbp, BorderLayout.NORTH);
            cards = new JPanel();
            cards.setLayout(new CardLayout());    


            JPanel p1 = new JPanel();
            p1.add(new JLabel("First panel!"));
            btn1 = new JButton("Text panel");
            btn1.addActionListener(this);
            p1.add(btn1);


            JPanel p2 = new JPanel();
            p2.add(new JLabel("Second panel!"));
            btn2 = new JButton("Button panel");
            btn2.addActionListener(this);
            p2.add(btn2);

            cards.add(p1, PANEL1);
            cards.add(p2, PANEL2);
            contentPane.add(cards, BorderLayout.CENTER);


        }

       public void init(){}

        public void actionPerformed(ActionEvent evt) {

            CardLayout cl = (CardLayout)(cards.getLayout());

                if(evt.getSource() == btn1)
                {
                    cl.show(cards, PANEL1);
                }
                else if(evt.getSource() == btn2)
                {
                    cl.show(cards, PANEL2);
                }   
           }
    }

Solution

  • The code that I've given is literally taken from that tutorial.

    So why don't your PANEL1 and PANEL2 variables have values? How do you expect it to work if you don't specify the name of the card??? How do you expect something to work if you don't copy working code? Why should we provide working code when you can't even follow the code from the tutorial? Why do you think any code we provide will be any different?

    but I don't understand how to make each card a different class.

    There is no difference. If you want to display a card you need to know the name of the card you want to display.

    Right now you use:

    JPanel p1 = new JPanel();
    cards.add(p1, PANEL1);
    

    So if the panel is in a different class then the code would be:

    JPanel p1 = new SomeCustomPanel()
    cards.add(p1, PANEL1);
    

    If you want all the code in a separate panel then you will need to get the layout manager from the parent parent panel so the code in the ActionListener of each panel might be something like:

    JPanel parent = childPanel.getParent();
    CardLayout layout = (CardLayout)parent.getLayout();
    

    Now you have access to the layout manager so you can switch cards assuming you know the name of each card. How you name each card and how you assign the card name is up to you.

    Edit:

    public class SomeCustomPanel extends JPanel
    {
        public SomeCustomPanel
        {
            add( new JLabel("I'm a JLabel") );
            JButton button = new JButton("Switch Panel");
            add(button);
            button.addActionListener(...); // add ActionListener to swap cards
        }
    }
    

    Edit:

    Well I used those names to try to show a parent/child relationship between comonents when you add components to a panel, or one panel to another panel.

    When you use an ActionListener the source of the ActionEvent will be the JButton. So given the button that generated ActionEvent you can go back up the parent/child chain until you find the panel using the CardLayout.

    So your code would be more like:

    JButton button = (JButton)e.getSource();
    JPanel someCustomPanel = (JPanel)button.getParent();
    JPanel cardLayoutPanel = (JPanel)someCustomPanel.getParent();
    CardLayout layout = (CardLayout)cardLayoutPanel.getLayout();
    layout.show(cardLayoutPanel, "...");
    

    Now that you have the layout manager you just need to know the name of the card you want to display