如何让JComboBox中的某些项目无法选择?

woe*_*ann 4 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,
        isSelected, cellHasFocus);

    if (not selectable conditions) {
        comp.setEnabled(false);
        comp.setFocusable(false);
    } else {
        comp.setEnabled(true);
        comp.setFocusable(true);
    }

    return comp;
}
Run Code Online (Sandbox Code Playgroud)

项目变为灰色,但仍可由用户选择.

Seu*_*ewa 5

选择"不可选择"项目时,尝试将所选项目更改为上次选择的项目.这意味着您需要在字段中存储"最后选择的项目".

  • 如果有人试图使用键盘来查看项目列表,那可能会产生不良影响.走向无法选择的人将永远反弹回到前一个. (3认同)