2010-03-04 63 views
4

如何讓我的一些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; 
} 

項目變灰,但仍可由用戶選擇。

回答

5

嘗試將選定項目更改爲上次選擇的項目時,選擇了「不可選」項目。這意味着您需要將「上次選擇的項目」存儲在一個字段中。

+3

如果有人試圖使用鍵盤沿着物品列表走下去,這可能會產生不好的效果。下降到不可選擇的一個將永遠反彈回到以前。 – DJClayworth 2011-07-13 16:29:32

1

我會試圖做到這一點的方式是隻向用戶顯示有效的項目,任何無效的東西都會隱形。我希望這有幫助。

+0

這就是我現在這樣做的方式...... – woezelmann 2010-03-04 12:06:04

相關問題