5 Replies Latest reply on Aug 12, 2006 4:05 PM by 843805

    JLabel with html tag can not be disabled or setForegroud?!

      I have some JLabel(s) with html tags, eg:
      JLable lbl1 = new JLabel();

      When I tried to disable the JLabel OR setForeGround to the JLabel, it doesn't work! (The label is not dimmed)

      Can anyone tell me how to disable the JLabel which has text with html tags? Thanks!
        • 1. Re: JLabel with html tag can not be disabled or setForegroud?!
          It's a known bug that is listed as "Closed, will not be fixed"
          Thanks for the heads up - I had just started to use HTML in a couple of labels myself, and I hadn't tested this part yet.
          • 2. Re: JLabel with html tag can not be disabled or setForegroud?!
            I'm not sure why you needed to use HTML in your labels, but I was using them for line-breaks so that a label could be multi-lined.

            I was able to work around the problem by sublassing JLabel, and instead of supplying HTML, I just gave it a regular String with "\n" wherever I wanted a line break. Then in the subclass, I had to override setText(), setEnabled(), and store the text value in a separate class variable.

            For those interested, here's a demo with the sublass MultilineLabel:
            import java.awt.*;
            import java.awt.event.*;
            import java.util.*;
            import javax.swing.*;
            public class MultilineLabelExample extends JFrame
              private JLabel mLabel =
                  new MultilineLabel("this text can span\nmultiple lines");
              private JLabel jLabel = new JLabel("standard label");
              private JButton btnBiasAdjuster = new JButton("Enable/Disable");
              public MultilineLabelExample()
                super("Multiline Label Example");
                btnBiasAdjuster.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent event)
                getContentPane().setLayout(new FlowLayout());
              public static void main(String args[])
                MultilineLabelExample f = new MultilineLabelExample();
                f.setSize(400, 200);
              public class MultilineLabel extends JLabel
                private String textValue;
                public MultilineLabel(String text)
                  textValue = text;
                public void setEnabled(boolean enabled)
                public void setText(String text)
                  textValue = text;
                  StringTokenizer tokenizer = new StringTokenizer(text, "\n", false);
                  StringBuffer buffer = new StringBuffer("<html><b>");
                  while (tokenizer.hasMoreTokens())
                private String getFontTag()
                  Color c = isEnabled() ? (Color) UIManager.get("Label.foreground") :
                                          (Color) UIManager.get("Label.disabledForeground");
                  return "<font color=\"" +
                          Integer.toHexString(c.getRed()) +
                          Integer.toHexString(c.getGreen()) +
                          Integer.toHexString(c.getBlue()) +
            I just wrote it, so I can't say that it's bullet-proof just yet. I just wanted to do a proof-of-concept thing.

            Let me know if anyone has a better solution, or improvements to this one.
            • 3. Re: JLabel with html tag can not be disabled or setForegroud?!
              when you disable the label, use the html font color tag to set the color to gray

              lbl1.setText("<html><font color = gray>My<br>Label</font></html>");
              • 4. Re: JLabel with html tag can not be disabled or setForegroud?!
                KelVarnson, thank you very much for sharing your MultilineLabel class. It's what I want.

                Michael_Dunn, thank you too.
                • 5. Re: JLabel with html tag can not be disabled or setForegroud?!
                  The HTML text rendering uses the component's foreground color by default, so in your class that extends the component's class you only need to override setEnabled(boolean b) and set the foreground color there, works fine whether the text is HTML or not e.g.
                  public void setEnabled(boolean b){
                          setForeground(b ? (Color) UIManager.get("Label.foreground") : (Color) UIManager.get("Label.disabledForeground"));