kdev
kdev

Reputation: 291

CardLayout not working properly

I have written this simple Cardlayout example with Splitpane, Combobox and few other panels containing buttons and label.

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class splitpane_test extends JFrame implements ItemListener {

    private JPanel contentPane;
    final static String BUTTONPANEL = "Card with JButtons";
    final static String TEXTPANEL = "Card with JTextField";
    JPanel cards;

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

            public void run() {
                try {
                    splitpane_test frame = new splitpane_test();
                    //frame.addComponentToPane(frame.getContentPane());
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public splitpane_test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JSplitPane splitPane = new JSplitPane();
        contentPane.add(splitPane, BorderLayout.CENTER);


        JPanel comboBoxPane = new JPanel();
        String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL};
        JComboBox cb = new JComboBox(comboBoxItems);
        cb.setEditable(false);
        cb.addItemListener(this);
        comboBoxPane.add(cb);
        splitPane.setLeftComponent(comboBoxPane);

        //Create the "cards".
        JPanel card1 = new JPanel();
        card1.add(new JButton("Button 1"));
        card1.add(new JButton("Button 2"));
        card1.add(new JButton("Button 3"));

        JPanel card2 = new JPanel();
        card2.add(new JTextField("TextField", 20));

        //Create the panel that contains the "cards".
        cards = new JPanel();
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);
        splitPane.setRightComponent(cards);
        cards.setLayout(new CardLayout(0, 0));
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub
        System.out.print("Event Triggered \n");
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, TEXTPANEL);
    }
}

I can see the splitpane with combobox on left and other cardlayout panels on right. when i change the combobox items nothing is happening on right size. In order to verify if iam hitting the cardout i used the System.out.print("Event Triggered \n"); but the surprising thing i have seen is that its displaying twice for each combobox item change as if its calling twice

Event Triggered
Event Triggered

Can you please suggest me what iam doing wrong here and why event triggered is getting hit twice. Thanks for all your time and help.

Upvotes: 4

Views: 2398

Answers (2)

trashgod
trashgod

Reputation: 205785

Can you please suggest me what I am doing wrong here and why event triggered is getting hit twice.

If you look at the ItemEvent, you'll see that one item is being DESELECTED and the other is being SELECTED. Instead, listen for ActionEvent, as shown here, and select the correct card accordingly.

Addendum: If you implement the helpful changes in @Michael Brewer-Davis's answer, then a suitable ActionListener is particularly straightforward:

@Override
public void actionPerformed(ActionEvent e) {
    JComboBox jcb = (JComboBox) e.getSource();
    CardLayout cl = (CardLayout) cards.getLayout();
    cl.show(cards, jcb.getSelectedItem().toString());
}

Upvotes: 4

Michael Brewer-Davis
Michael Brewer-Davis

Reputation: 14276

  1. Set the layout manager before adding components.

  2. Two items are changing state; one is being deselected, the other selected. You would improve your debugging output with the following:

    System.out.println("Event Triggered: " + e);

You'll also need to account for the event switching the selection back--not all changes in the combo box should select TEXTPANEL.

Upvotes: 3

Related Questions