0

I am trying to switch between different panels using JComboBox and CardLayout but any switching doesn't occur. Printing the ItemSelected was revealed that ItemListener is working correctly and also the accuracy of card layout was confirmed by examining other methods like next(), previous(), ...

I would really appreciate if anybody can help me out with this issue.

public class MyPanel exends JPanel {

public MyPanel() {
    setBodyPanel();
}
private void setBodyPanel() {
    card = new JPanel(new CardLayout());
    cards.add(noBodyPanel);
    cards.add(formPanel);
    cards.add(jsonPanel);
    cards.add(binaryFilePanel);

    String comboBoxItems[] = {"No Body", "Form Data", "JSON", "Binary Data"};
    JComboBox cbBodyType = new JComboBox(comboBoxItems);
    cbBodyType.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            CardLayout cl = (CardLayout) cards.getLayout();
            if (e.getStateChange() == ItemEvent.SELECTED) {
                cl.show(cards,e.getItem().toString());
                System.out.println(e.getItem().toString());
            }
        }
    });
    JPanel cbPanel = new JPanel();
    cbPanel.add(cbBodyType);
    add(cbPanel,BorderLayout.SOUTH);
    add(cards,BorderLayout.CENTER);
}}

I deleted the unnecessary codes

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
shiva
  • 11
  • 5
  • You need to add the `JPanel` with the same name you use to retrieve it: `add("No Body", noBodyPanel);`. See a [working example](https://stackoverflow.com/a/46013230/3992939) – c0der May 14 '20 at 12:43
  • 1
    *"I deleted the unnecessary codes"* For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 14 '20 at 13:04

1 Answers1

1

You need to add the JPanel with the same name you use to retrieve it.
Here is an mre1 demonstrating it :

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.event.ItemEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        JFrame frame=new JFrame();
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.add(new MyPanel());
        frame.pack();
        frame.setVisible(true);
    }
}

class MyPanel extends JPanel {

    public MyPanel() {
        setLayout(new BorderLayout(10,10));
        setBodyPanel();
    }
    private void setBodyPanel() {

        String comboBoxItems[] = {"No Body", "Form Data", "JSON", "Binary Data"};
        JPanel cards = new JPanel(new CardLayout());

        for(String name : comboBoxItems){
            cards.add(name, getPanel(name));
        }

        JComboBox<String> cbBodyType = new JComboBox<>(comboBoxItems);
        cbBodyType.addItemListener(e -> {
            CardLayout cl = (CardLayout) cards.getLayout();
            if (e.getStateChange() == ItemEvent.SELECTED) {
                cl.show(cards,(String)e.getItem());
            }
        });

        add(cards,BorderLayout.CENTER);
        JPanel cbPanel = new JPanel();
        cbPanel.add(cbBodyType);
        add(cbPanel,BorderLayout.PAGE_END);
    }

    private Component getPanel(String name) {
        JPanel p = new JPanel();
        p.add(new JLabel(name +" panel"));
        return p;
    }
}


1 It is highly recommended to post mre or SSCCE when asking and answering
c0der
  • 18,467
  • 6
  • 33
  • 65