Forum Stats

  • 3,875,931 Users
  • 2,266,994 Discussions


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

843805 Member Posts: 49,999 Green Ribbon
edited Aug 12, 2006 12:05PM in Swing
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!


  • 843805
    843805 Member Posts: 49,999 Green Ribbon
    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.
  • 843805
    843805 Member Posts: 49,999 Green Ribbon
    edited May 11, 2006 11:07AM
    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.
  • 794342
    794342 Member Posts: 6,422
    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>");
  • 843805
    843805 Member Posts: 49,999 Green Ribbon
    KelVarnson, thank you very much for sharing your MultilineLabel class. It's what I want.

    Michael_Dunn, thank you too.
  • 843805
    843805 Member Posts: 49,999 Green Ribbon
    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"));
This discussion has been closed.