java如何使JComboBox中的某些项不可选择?
如何使某些JComboBox项不可选择?我试过这个:
@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;
}
项目变为灰色,但用户仍可以选择
# 1 楼答案
选择“不可选择”项目时,尝试将所选项目更改为最后一个所选项目。这意味着您需要在字段中存储“最后选择的项目”
# 2 楼答案
我想这样做的方式是只向用户显示有效项,任何无效项都会使其不可见。我希望这有帮助