2

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.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • For better help sooner, post an [SSCCE](http://sscce.org/). (It needs a `main(String[])` that puts it on-screen to make it an SSCCE.) – Andrew Thompson Nov 06 '12 at 10:44
  • does the strike-through work on a plain label (that is when not used as renderer)? Hach ... just saw the jre1.5: _that_ might be the problem. None installed here, so can't test - but if you can verify that it's the version your only option might be to convince them to update to a newer version. – kleopatra Nov 06 '12 at 10:56
  • @kleopatra - strike-through on a plain label has the exact same problem. JRE 1.5 shouldn't be the problem - [strike-through appears in the 1.5 docs](http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/font/TextAttribute.html#STRIKETHROUGH). I also tried using a different font, thinking the default font doesn't support strikethrough. I tried setting the font to `"Monospace", Font.PLAIN, 14.0f`, but that didn't fix the problem either; still worked on the windows machine but not on solaris.Any other suggestions? Could screen resolution be the culprit? – user1614284 Nov 06 '12 at 13:02
  • no idea, sorry (but good to know that it's not related to the jdk version :-) – kleopatra Nov 06 '12 at 13:14
  • I think that you ahve to get those attributes from Font not on fly, `Map attributes = (new Font("Serif", Font.PLAIN, 12)).getAttributes(); //attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD); attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);` – mKorbel Nov 06 '12 at 13:26
  • @mKorbel - I tried creating a new, "clean", font object as you suggest. Interestingly enough, the font itself (and the weight, when I checked) changes as expected. It's just the strike-through that isn't working properly when running on Solaris. – user1614284 Nov 06 '12 at 13:43
  • @user1614284 total ZOO :-), please is possible to use Html strike-through in JLabel or (J)Component returns from XxxRenderer, because that could be last property, maybe is there issue Solaris v.s.Fonts, you can to test for [visual output (add there strike-through) to the HightLighter](http://stackoverflow.com/a/9022901/714968) – mKorbel Nov 06 '12 at 14:02
  • [aaaaach by @darryl burke](http://tips4java.wordpress.com/2008/11/30/visual-font-designer/) – mKorbel Nov 06 '12 at 14:05
  • @mKorbel - using HTML works for some reason (can anyone explain this?). However, using HTML seems to change the inner padding of the Component, so the result is some components with strike-through but aligned slightly off vertical center, and some components without strike-through but properly aligned. I didn't quite understand what you are suggesting I do with the HighLighter. I checked the docs, and it looks like it's meant to be used to change background coloring. – user1614284 Nov 06 '12 at 14:26
  • this code loading all fonts from local enviroment, add attributes to the MutableXxxXxx, better could be to use code that I linked, logics is better, because there are returned all available attributes for selected font, please whats your solaris config – mKorbel Nov 06 '12 at 14:34
  • @mKorbel - re. the Highlighter, I added strike-through to the MutableAttributeSet and ran it on the solaris machine. All fonts appeared with a strike-through in them. Re. my solaris config - no idea. It's someone else's machine that I have on loan for this project. Other than that it's a custom image based on Solaris 10 and had jre 1.5 on it, I don't know much else. Anything I should check for? – user1614284 Nov 06 '12 at 14:57
  • probably there must be another issue, try to call repaint() before returns JLabel Object from XxxRenderer, otherwise you.'ll need to use Html and swingconstant.center for relayout the JLabel – mKorbel Nov 06 '12 at 15:26

0 Answers0