I'm using a JList as part of a wizard to display all the steps to be performed (it also allows clicking on a step to go to it). Some steps will not always be needed, based on what's done in previous steps. It's these inapplicable steps I'd like to disable in the list.
How can I go about disabling (preventing the selection of) certain items in the list? Is there a better way than subclassing JList and overriding every selection-related method?
I wanted a JList with cells that couldn't be select AND were transparent. So here's what I did:
class DisabledItemListCellRenderer extends JLabel implements ListCellRenderer<Object> {
private static final long serialVersionUID = 1L;
public DisabledItemListCellRenderer() {
setOpaque(false);
}
@Override
public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
String txt = (String) value;
setText(txt);
return this;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With