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!
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:
Disable:
Example: