views:

238

answers:

2

How can I make some of my JComboBox Items not Selectable? I tried this:

@Override    
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

      if (not selectable conditions){
         comp.setEnabled(false);
         comp.setFocusable(false);
      } else {
         comp.setEnabled(true);
         comp.setFocusable(true);
      }

      return comp;
}

The items become gray, but are still selectable by the user.

+1  A: 

The way I would be tempted to do this would be to only show the user(s) the valid items, anything invalid make invisible. I hope this helps.

Neurofluxation
this is the way i am doing it right now...
woezelmann
+1  A: 

Try changing the selected item to the last selected item when an 'unselectable' item is selected. That means you need to store the 'last selected item' in a field.

Seun Osewa