4
这是一个相当常见的问题,我使用的解决方案类似于what I searched,稍后会发现。一个实现了JLabel
启用或禁用本身基于当前选择的指数ListCellRenderer
:禁用单个JComboBox项目
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setText(value.toString());
UIDefaults defaults = UIManager.getDefaults();
Color fc;
if (index == 1) {
setEnabled(false);
fc = defaults.getColor("Label.disabledForeground");
setFocusable(false);
} else {
// fc = defaults.getColor("Label.foreground");
fc = list.getForeground();
setEnabled(list.isEnabled());
setFocusable(true);
}
setForeground(fc);
setBackground(isSelected ? list.getSelectionBackground() : list
.getBackground());
return this;
}
的问题是,即使在视觉列表项显示为禁用,它仍然可以尽管setFocusable
呼叫选择。 我该如何真正禁用它?