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