How can I make some items in a JComboBox unselectable?
How can I make some of my JComboBox Items unselectable? 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);
} e开发者_如何学JAVAlse {
comp.setEnabled(true);
comp.setFocusable(true);
}
return comp;
}
The items become gray, but are still selectable by the user.
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.
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.
精彩评论