I have a JList containing instances of a class Operation which contains a boolean flag "enabled". When this flag is set to false, I want the text representation of the Operation instance in the JList to have TextAttribute.STRIKETHROUGH_ON set.
To achieve this, I implemented a custom ListCellRenderer which extends DefaultListCellRenderer:
package com.pumdashboard.ui.configurator;
import java.awt.Component;
import java.awt.font.TextAttribute;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import com.pumdashboard.data.Operation;
@SuppressWarnings ("serial")
public class OperationsListCellRenderer extends DefaultListCellRenderer {
@SuppressWarnings ("rawtypes")
@Override
public Component getListCellRendererComponent (final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
final JLabel cmpnnt = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value == null) {
return cmpnnt;
}
final Operation op = (Operation) value;
if (!op.get_enabled()) {
final Map<TextAttribute, Object> attr = new HashMap<TextAttribute, Object>(list.getFont().getAttributes());
attr.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
cmpnnt.setFont(list.getFont().deriveFont(attr));
}
return cmpnnt;
}
}
This implementation works just fine on Windows 7 in eclipse 4.2 running JDK 1.7 set to 1.5 mode. However, when I try to run this exact same code on Solaris 10 with JRE 1.5, no strikethrough appears.
Any help would be greatly appreciated.