6

In dialog I need to display one group of controls if some combo is checked and another group of controls otherwise. I.e. I need 2 layers and I need to switch between them when combo is checked/unchecked. How can I do that?

Thanks

Simley Jo
  • 33
  • 8
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305

1 Answers1

14

CardLayout works well for this, as suggested below.

enter image description here

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/questions/6432170 */
public class CardPanel extends JPanel {

    private static final Random random = new Random();
    private static final JPanel cards = new JPanel(new CardLayout());
    private static final JComboBox combo = new JComboBox();
    private final String name;

    public CardPanel(String name) {
        this.name = name;
        this.setPreferredSize(new Dimension(320, 240));
        this.setBackground(new Color(random.nextInt()));
        this.add(new JLabel(name));
    }

    @Override
    public String toString() {
        return name;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 1; i < 9; i++) {
            CardPanel p = new CardPanel("Panel " + String.valueOf(i));
            combo.addItem(p);
            cards.add(p, p.toString());
        }
        JPanel control = new JPanel();
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox jcb = (JComboBox) e.getSource();
                CardLayout cl = (CardLayout) cards.getLayout();
                cl.show(cards, jcb.getSelectedItem().toString());
            }
        });
        control.add(combo);
        f.add(cards, BorderLayout.CENTER);
        f.add(control, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 2
    See also this related [example](http://stackoverflow.com/questions/5654926/implementing-back-forward-buttons-in-swing/5655843#5655843). – trashgod Jun 22 '11 at 20:42
  • This example extends `JPanel` to add a name, but [`Component`](http://download.oracle.com/javase/6/docs/api/java/awt/Component.html) has `getName()` and `setName()` methods as an alternative. – trashgod Jul 01 '11 at 03:19
  • what I see, congrats, +++ second real Swing Guru +++, gooood – mKorbel Jul 01 '11 at 05:59
  • @nIcEcOw: Thank you for commenting; I've also been experimenting with `Color.getHSBColor`, for [example](http://stackoverflow.com/a/18833151/230513). – trashgod Oct 04 '13 at 18:17
  • [`setPreferredSize()`](http://stackoverflow.com/q/7229226/230513) used for demonstration purposes only. – trashgod Jul 27 '14 at 11:04