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

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

    843805
      I have some JLabel(s) with html tags, eg:
      JLable lbl1 = new JLabel();
      lbl1.setText("<html>My<br>Label</html>");

      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?!
          843805
          It's a known bug that is listed as "Closed, will not be fixed"
          http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4303706
          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?!
            843805
            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)
                  {
                    mLabel.setEnabled(!mLabel.isEnabled());
                    jLabel.setEnabled(!jLabel.isEnabled());
                  }
                });
            
                getContentPane().setLayout(new FlowLayout());
                getContentPane().add(mLabel);
                getContentPane().add(jLabel);
                getContentPane().add(btnBiasAdjuster);
              }
            
              public static void main(String args[])
              {
                MultilineLabelExample f = new MultilineLabelExample();
            
                f.setDefaultCloseOperation(EXIT_ON_CLOSE);
                f.setSize(400, 200);
                f.setVisible(true);
              }
            
              public class MultilineLabel extends JLabel
              {
                private String textValue;
            
                public MultilineLabel(String text)
                {
                  super(text);
                  textValue = text;
                }
            
                public void setEnabled(boolean enabled)
                {
                  super.setEnabled(enabled);
                  setText(textValue);
                }
            
                public void setText(String text)
                {
                  textValue = text;
            
                  StringTokenizer tokenizer = new StringTokenizer(text, "\n", false);
            
                  StringBuffer buffer = new StringBuffer("<html><b>");
                  buffer.append(getFontTag());
                  buffer.append(tokenizer.nextToken());
            
                  while (tokenizer.hasMoreTokens())
                  {
                    buffer.append("<br>");
                    buffer.append(tokenizer.nextToken());
                  }
            
                  buffer.append("</font></b></html>");
            
                  super.setText(buffer.toString());
                }
            
                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?!
              794342
              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?!
                843805
                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?!
                  843805
                  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){
                          super.setEnabled(b);
                          setForeground(b ? (Color) UIManager.get("Label.foreground") : (Color) UIManager.get("Label.disabledForeground"));
                  }