How to view another jPanel containing many sub-jPanels from same JFrame in netbeans (Java Swing)

When I use CardLayout, I am able to use only one panel at a time, isn’t there a way to add multiple panels in one frame and then after an event switch to another set of multiple panels within same frame?

Exactly, you can show only one JPanel everytime with CardLayout but that doesn’t prevent you to show multiple JPanels when using it…

You need to make the card (the JPanel that is shown in the current view) to show multiple JPanels.

For example:

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

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

public class CardLayoutWithMultiplePanes {
    private JFrame frame;
    private JPanel pane;
    private JPanel cardsPane;
    private JPanel[] cards;
    private CardLayout cl;
    private JButton nextButton;
    private JButton previousButton;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CardLayoutWithMultiplePanes()::createAndShowGui);
    }
    
    private void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());
        pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
        
        previousButton = new JButton("Previous");
        nextButton = new JButton("Next");
        
        cl = new CardLayout();
        cardsPane = new JPanel(cl);
        cards = new JPanel[2];
        
        for (int i = 0; i < cards.length; i++) {
            cards[i] = new JPanel();
            cards[i].setLayout(new GridLayout(2, 1));
            cards[i].add(new CustomPane((i + 1) % 2 == 0 ? Color.BLUE : Color.RED));
            cards[i].add(new CustomPane((i + 1) % 2 == 0 ? Color.GREEN : Color.MAGENTA));
            
            cardsPane.add(cards[i]);
        }
        
        Box box = Box.createHorizontalBox();
        box.add(previousButton);
        box.add(Box.createHorizontalGlue());
        box.add(nextButton);
        
        previousButton.addActionListener(listener);
        nextButton.addActionListener(listener);
        
        pane.add(cardsPane);
        pane.add(box);
        
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private ActionListener listener = e -> {
        if (e.getSource().equals(previousButton)) {
            cl.previous(cardsPane);
        } else if (e.getSource().equals(nextButton)) {
            cl.next(cardsPane);
        }
    };
    
    @SuppressWarnings("serial")
    class CustomPane extends JPanel {
        private Color color;
        
        public CustomPane(Color color) {
            this.color = color;
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
        
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }
    }
}

enter image description here enter image description here

The above code shows a single JPanel that contains 2 more JPanels, in which each JPanel has its own background color (and might contain their own components such as JLabel or JButton, etc)

I hope this gives you an idea for what you’re trying to do.

Note:

  • Imagine your JFrame as a notebook.
  • Imagine the JPanels as the sheets.
  • Imagine CardLayout as your finger passing pages (back and forward)

In every sheet (JPanel) you can have whatever you want (even more than 1 sheet (glued to it)), it’s the same principle here

Leave a Comment