Search code examples
javaswingjcomboboxitemlistener

JComboBox changes components only when going from 1st to 2nd (Not from 3rd to 2nd)


So I tried searching for a solution but found nothing and didn't know how to make my search more accurate. I have this for homework in Java and I got stuck with a JComboBox that doesn't always change the components in my JPanel. This is my first time posting a question so sorry if I mess up with this. Anyhow, this is the code (The purpose is to create GUI for a flight company).

The problem is that when I go from option 1("office") to option 2("Pilot"), the JComboBox does what it should do and changes the JPanel (jplType) as should, but when I go from option 3("Host") back to option 2, it doesn't change the JPanel (jplType) at all.

The thing is, I put a System.out.println() in the

if(index == 1)
{
  // ..
}

and it does print it but for some reason it fails to add the JCheckBox.

I'd be grateful if someone can figure out what I messed up here because I've been at it for an hour.

Thanks in advance.

public class abc {

public static void main(String[] args) {
    CompanyDialog abc = new CompanyDialog();

    }

}

class CompanyDialog extends JFrame {

    private String[] employee = {"office", "Pilot", "Host"};
    private String[] employeeTitles = {"Office options", "Pilot options", "Host options"};
    public final static int GAP = 3;
    // Frame Constants.
    public final static int FWIDTH = 605;
    public final static int FHEIGHT = 235;
    // Constant for the Combo Box.
    public final static int CBWIDTH = FWIDTH/3-4;
    public final static int CBHEIGHT = 20;
    // Width constant for TextField.
    public final static int TWIDTH = 20;

    private JPanel jplType = new JPanel(new FlowLayout(FlowLayout.LEFT, GAP, GAP));

    private JComboBox jcbType = new JComboBox(employee);
    private JLabel jlblOptions = new JLabel();
    private JTextField jtfOptions = new JTextField(TWIDTH);

public CompanyDialog(){
    super("Worker Dialog");

    //Combo box panel configurations
    JPanel jplWorker = new JPanel(new FlowLayout(FlowLayout.LEFT, GAP, GAP));

    JTextField nametxt = new JTextField(TWIDTH);
    jcbType.setPreferredSize(new Dimension(CBWIDTH,CBHEIGHT));
    jplWorker.add(jcbType);

    setDisplay(0);

    add(jplWorker, BorderLayout.NORTH);
    setAlwaysOnTop(true);
    setVisible(true);
    setSize(FWIDTH, FHEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setResizable(false);

    jcbType.addItemListener(new ItemListener() { /**Handle item selection*/
        public void itemStateChanged(ItemEvent e) {
            jplType.removeAll();
            setDisplay(jcbType.getSelectedIndex());
        }
    }
            );
}
public void setDisplay(int index){
    jplType.add(jlblOptions);
    jplType.add(jtfOptions);
    jplType.setBorder(new TitledBorder(employeeTitles[index]));
    if(index == 0){
        jlblOptions.setText("Office:");
        jtfOptions.setText("");
    }
    else if(index == 1) {
        jlblOptions.setText("Air Time:");
        jtfOptions.setText("0");
        JCheckBox jcbCaptain = new JCheckBox("Is a Captain");
        jplType.add(jcbCaptain);
    }
    else if(index == 2){
        jlblOptions.setText("Air Time:");
        jtfOptions.setText("0");
    }

    add(jplType, BorderLayout.CENTER);
}
}

Solution

  • Change your setDisplay() Method from

    public void setDisplay(int index){
            jplType.add(jlblOptions);
            jplType.add(jtfOptions);
            jplType.setBorder(new TitledBorder(employeeTitles[index]));
            if(index == 0){
                jlblOptions.setText("Office:");
                jtfOptions.setText("");
            }
            else if(index == 1) {
                jlblOptions.setText("Air Time:");
                jtfOptions.setText("0");
                JCheckBox jcbCaptain = new JCheckBox("Is a Captain");
                jplType.add(jcbCaptain);
            }
            else if(index == 2){
                jlblOptions.setText("Air Time:");
                jtfOptions.setText("0");
            } 
            else
            {
                jlblOptions.setText("");
                jtfOptions.setText("");
            }
            add(jplType, BorderLayout.CENTER);
    
         }
    

    to

    public void setDisplay(int index){
            jplType.add(jlblOptions);
            jplType.add(jtfOptions);
            jplType.setBorder(new TitledBorder(employeeTitles[index]));
            if(index == 0){
                jlblOptions.setText("Office:");
                jtfOptions.setText("");
            }
            else if(index == 1) {
                jlblOptions.setText("Air Time:");
                jtfOptions.setText("0");
                JCheckBox jcbCaptain = new JCheckBox("Is a Captain");
                jplType.add(jcbCaptain);
            }
            else if(index == 2){
                jlblOptions.setText("Air Time:");
                jtfOptions.setText("0");
            } 
            else
            {
                jlblOptions.setText("");
                jtfOptions.setText("");
            }
            add(jplType, BorderLayout.CENTER);
    
            this.revalidate();
        }