Reputation: 53
I habe a Java-Problem. I made a code which includes a Checkbox (mPortalJCheckbox) and Combobox (mVersandComboBox).
When the Checkbox is true, there should only be 2 items in mVersandComboBox. Otherwise there should be 3 items.
My Listener is like this:
mPortalJCheckbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (mPortalJCheckbox.isSelected() == false) {
System.out.println(mPortalJCheckbox.isSelected());
mVersandComboBox.removeItemAt(2);
mVersandComboBox.revalidate();
mVersandComboBox.repaint();
}
}
});
I think the Combobox removes the last item but it doesn't repaint the Combobox right. Where is my mistake?
Thanks :)
Upvotes: 0
Views: 1289
Reputation: 324098
You don't need revalidate() or repaint(). The comboBox will repaint itself when data in the combo box model is changed.
The removeItemAt(...)
method only works if the ComboBoxModel is mutable. Make sure you are using the DefaultComboBoxModel:
DefaultComboBoxModel model = new DefaultComboBoxModel(...);
JComboBox comboBox = new JComboBox( model );
If you need more help then post a proper SSCCE that demonstrates the problem.
Upvotes: 1