Deshabilitando elementos individuales de JComboBox
Este es un problema bastante común, y la solución que he usado es similar alo que busqué y encontrado más tarde. Uno implementa unListCellRenderer
con unJLabel
que se activa o desactiva en función del índice seleccionado actual:
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;
}
El problema es que aunque visualmente el elemento de la lista aparece como deshabilitado, aún puede seleccionarse a pesar desetFocusable
llamada. ¿Cómo lo deshabilito realmente?