Question

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);
    } else {
        comp.setEnabled(true);
        comp.setFocusable(true);
    }

    return comp;
}

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top