views:

362

answers:

4

How do I get a JLabel displaying a HTML string to appear greyed out (which is the behaviour of JLabels that don't display HTML text)? Is there another way than actually changing the colour myself by modifying the foreground property?

JLabel label1 = new JLabel("Normal text");
JLabel label2 = new JLabel("<html>HTML <b>text</b>");
// Both labels are now black in colour

label1.setEnabled(false);
label2.setEnabled(false);
// label1 is greyed out, label2 is still black in colour

Thank you very much for all of your responses. From what I gather, it seems that Java doesn't support automatic greying out of JLabels when they use HTML text. Suraj's solution has come closest to the fix considering the limitations.

I have however, tried a different out-of-the box approach, where I have put the HTML text JLabels inside of an inner JPanel and did this:

mInnerPanel.setEnabled(shouldShow); //shouldShow is a boolean value

Which hasn't worked. Any suggestions for this way?


EDIT: Added implemented solution.

A: 

You can specify the font color in the HTML.

camickr
@camickr: So I would have to do this every time the enabled / disabled state changes?
bguiz
Yes, which is why using the setForeground() method is still the easiest solution.
camickr
+6  A: 

If text is HTML, the text wont be grayed out because of the following code in BasicLabelUI#paint()

        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
        if (v != null) {
        v.paint(g, paintTextR);
        }

As you can see if the text is html, then the View is used to paint and it is not checked wheter the label is enabled or not. Hence we need to do it explictly as shown below:

label2.addPropertyChangeListener(new PropertyChangeListener() {
   public void propertyChange(PropertyChangeEvent evt) {
    if (!evt.getPropertyName().equals("enabled"))
     return;
    if (evt.getNewValue().equals(Boolean.FALSE))
     label2.setText("<html><font color=gray>HTML <b>text</b></html>");
    else
     label2.setText("<html><font color=black>HTML <b>text</b></html>");
   }
  });
Suraj Chandran
`setForeground` works, so I'd suggest using that rather than altering the label text to change the colour.
lins314159
A: 

Override the paint method in the UI, set the client property BasicHTML.propertyKey to null if it is disabled and call super...

sreejith
@sreejith...apparently it will not work, for two reasons a) if you set BasicHTML.propertyKey to null then any component using html rendering will fail to paint html b) though the text will be displayed in gray, it will display the entrie html i.e. it will display "<html>HTML <b>text</b>" instead of HTML text. :)
Suraj Chandran
+1  A: 

Implemented solution:

    Color foreground = (shouldShow) ? SystemColor.textText : SystemColor.textInactiveText;
    for (Component comp : mInnerPanel.getComponents())
    {
        comp.setForeground(foreground);
    }

Caved in and used setForeground in the end, as it appears that Java seems to explicitly ignore the enabled property when painting JLabels so long as it contains HTML text. See also @Suraj's answer, for "pure" solution.

bguiz