0

I want to show another jPanel from a button event action. e.g.

private void jButtonMouseClicked(MouseEvent e)
{
    getContentPane().removeAll();
    update(getGraphics());
    //code to show another jPanel containing different sub-panels
}
  • 2
    Use a [`CardLayout`](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html#card), see this [example](https://stackoverflow.com/questions/34633999/how-to-add-action-to-a-button/34637817#34637817) to give you an idea – Frakcool Mar 27 '18 at 15:57
  • Anything I can do in `AbsoluteLayout`? @Frakcool – Arihant Bedagkar Mar 27 '18 at 16:02
  • Don't use it. `AbsoluteLayout` = `null layout` = [Null layout is evil](http://www.fredosaurus.com/notes-java/GUI/layouts/nulllayout.html) and [frowned upon to use them](https://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing)... Here's an [example](https://stackoverflow.com/questions/42520492/jtable-not-showing-up-on-jframe-java/42521097#42521097) of what happens when you use them... – Frakcool Mar 27 '18 at 16:04
  • Ok, thanks for the help! – Arihant Bedagkar Mar 27 '18 at 16:06
  • 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? @Frakcool – Arihant Bedagkar Mar 27 '18 at 16:58
  • Use GridBag layout to hold more than one sub components. Use a CardLayout to switch cards. It is best to use a GUI editor to place the components. It will generate complex Gridbag Layout for you. Never use absolute layout unless it is a quick prototype. – SamwellTarly Mar 27 '18 at 17:29

1 Answers1

1

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

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89