You need to somehow deny ComboBoxthe ability to set items that cannot be selected from the selected ones.
The easiest way I can come up with is to catch the change in choice in the model itself.
public class MyComboBoxModel extends DefaultComboBoxModel {
public MyComboBoxModel() {
addElement("Select me");
addElement("I can be selected");
addElement("Leave me alone");
addElement("Hit me!!");
}
@Override
public void setSelectedItem(Object anObject) {
if (anObject != null) {
if (!anObject.toString().equals("Leave me alone")) {
super.setSelectedItem(anObject);
}
} else {
super.setSelectedItem(anObject);
}
}
}
, . , items . , , - item, isSelectable.
ComboBoxModel, , items, model.contains(item) , , .