0

I'm trying to have an action listener add a combo box to a control panel when only when a check box is selected. For example, the project I'm currently working on is a t-shirt order form. A check box for "Graphic" is available. When "Monogram" is checked, a combo box "Graphic Style" should appear. How can I go about doing this? Nothing I've tried so far has worked.

Here is my method for building the checkbox

public JPanel createGraphicCheckBox()
{
    graphicCheck = new JCheckBox("Graphic");

    ActionListener listener = new MyListener();
    graphicCheck.addActionListener(listener);

    JPanel panel = new JPanel();

    //add check box to panel
    panel.add(graphicCheck);

    panel.setBorder(new TitledBorder(new EtchedBorder(), "Graphic"));

    return panel;
}

Here is my method for building the combo box

public JPanel createGraphSelectComboBox()
{
    graphSelectBox = new JComboBox();

    //Fills graphics combo box
    graphSelectBox.addItem("Select Graphic");
    graphSelectBox.addItem("AB20");
    graphSelectBox.addItem("JM17");
    graphSelectBox.addItem("PJ23");
    graphSelectBox.addItem("TR16");
    graphSelectBox.addItem("JK52");
    graphSelectBox.setEditable(true);

    JPanel panel = new JPanel();

    panel.add(graphSelectBox);

    return panel;
}

And here is my class for my listener

class MyListener implementes ActionListener {
    public void actionPerformed(ActionEvent e)
    {
      if(e.getSource() == monoCheck)
        {
            if(monoCheck.isSelected())
            {
                JPanel comboBoxPanel5 = createGraphSelectComboBox();

                controlPanel.add(comboBoxPanel5);

                revalidate();
            }
        }
     }
 }

Thank you guys for all the help you provide!

  • And what is happening right now? Why are you wrapping your combobox into additional panel? This can be a problem as no layout manager is set for that panel and nobody knows what will happen. Did you tried to remove that additional panel? – Antoniossss Nov 18 '14 at 09:05
  • implements is spelt wrong, probably not the problem though – Kevvvvyp Nov 18 '14 at 09:17
  • you're checking for `e.getSource() == monoCheck`, while the checkbox is called graphicCheck? – geert3 Nov 18 '14 at 09:35

1 Answers1

0

You can disable the JCheckbox when the program starts, and then you can set an actionlistener over your "Monogram"-Checkbox for example Mouse Clicked or Mouse Pressed. Then in this actionlistener you can enable your JCheckbox. The code for disable an enable a JCheckbox is:

Enable:

your_checkbox.setEnabled(true);

Disable:

your_checkbox.setEnabled(false);

Example:

if(monogram_checkbox.isSelected()){
    your_checkbox.setEnabled(true);
} else {
    your_checkbox.setEnabled(false);
}
Pascal
  • 1,255
  • 5
  • 20
  • 44