1
我有一個JList
,它連接到自定義DefaultListModel
,我想在組件的某些值上更改鼠標光標(取決於項目/值的類型)。在某些項目上,它應該是默認的光標,在其他某個手形光標上。將光標更改爲單個JList值
我認爲我可以在我的自定義DefaultListCellRenderer
做到這一點:
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
MyItem item = (MyItem) value;
setText(item.getDisplay());
if (!item.getType().equals("i"))
setCursor(new Cursor(Cursor.HAND_CURSOR)); // <-- doesn’t work
return this;
}
我的做法是行不通的。任何建議如何以正確的方式做到這一點?
非常感謝。很棒。 – laserbrain
+1,請考慮將此UI初始化代碼移至SwingUtilities.invokeLater塊 –
@GuillaumePolet即使在EDT之外,像mine這樣的小示例也將始終保持穩定。 –