Java 如何使JComboBox中的某些项不可选择?
如何使某些JComboBox项不可选择?我试过这个:Java 如何使JComboBox中的某些项不可选择?,java,swing,jcombobox,Java,Swing,Jcombobox,如何使某些JComboBox项不可选择?我试过这个: @Override public Component getListCellRendererComponent(JList list, Object value, int index. boolean isSelected, boolean cellHasFocus) { Component comp = super.getListCellRendererComponent(list, value, index,
@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;
}
项目变为灰色,但用户仍然可以选择。我尝试这样做的方式是只向用户显示有效的项目,任何无效的项目都将不可见。我希望这会有所帮助。当选择了“不可选择”项目时,尝试将所选项目更改为最后一个所选项目。这意味着您需要将“最后选定的项目”存储在字段中。如果有人试图使用键盘查看项目列表,则可能会产生不良影响。下到不可选择的一个将永远反弹到前一个。