How can I make some elements in JComboBox inaccessible?

How can I make some of my JComboBox items inaccessible? 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; } 

Elements turn gray, but are still available to the user.

+4
source share
2 answers

Try changing the selected item to the last selected item when the "non-selectable" option is selected. This means that you need to save the "last selected item" in the field.

+5
source

The way I would be tempted to do this was to show only the user (s) the actual elements, something invalid to make invisible. Hope this helps.

+1
source

Source: https://habr.com/ru/post/1303112/


All Articles