简体   繁体   English

显示HTML文本时,JLabel在禁用时不会显示为灰色

[英]JLabel not greyed out when disabled, when HTML text displayed

How do I get a JLabel displaying a HTML string to appear greyed out (which is the behaviour of JLabel s that don't display HTML text)? 如何让显示HTML字符串的JLabel显示为灰色(这是JLabel不显示HTML文本的行为)? Is there another way than actually changing the colour myself by modifying the foreground property? 还有其他方法,而不是通过修改foreground属性来实际改变颜色吗?

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 JLabel s when they use HTML text. 从我收集的内容来看,Java似乎不支持在使​​用HTML文本时自动使JLabel变灰。 Suraj's solution has come closest to the fix considering the limitations. 考虑到局限性, Suraj的解决方案最接近修复。

I have however, tried a different out-of-the box approach, where I have put the HTML text JLabel s inside of an inner JPanel and did this: 但是,我尝试了一种不同的开箱即用方法,我将HTML文本JLabel放在内部JPanel并执行此操作:

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

Which hasn't worked. 哪个没用。 Any suggestions for this way? 对这种方式有什么建议吗?


EDIT: Added implemented solution . 编辑:添加实施的解决方案

If text is HTML, the text wont be grayed out because of the following code in BasicLabelUI#paint() 如果文本是HTML,则由于BasicLabelUI#paint()中的以下代码,文本将不会显示为灰色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. 正如您可以看到文本是否为html,然后使用View进行绘制,并且不会检查标签是否已启用。 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>");
   }
  });

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 JLabel s so long as it contains HTML text. 陷入困境并最后使用setForeground ,因为看起来Java似乎在绘制JLabel时显式忽略enabled属性,只要它包含HTML文本即可。 See also @Suraj's answer , for "pure" solution. 另请参阅@ Suraj的答案 ,了解“纯粹”的解决方案。

I would suggest the following, which is combination of two solutions provided here: 我建议如下,这是这里提供的两种解决方案的组合:

public class HtmlLabel extends JLabel{
    public void setEnabled(boolean enabled){
        if(getClientProperty(BasicHTML.propertyKey) != null ){
            Color foreground = (enabled) ? SystemColor.textText : SystemColor.textInactiveText;
            setForeground(foreground);
        }
        super.setEnabled(enabled);
    }
}

您可以在HTML中指定字体颜色。

覆盖UI中的paint方法,如果禁用了客户端属性BasicHTML.propertyKey ,则将其设置为null并调用super ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM