Java 7, color of button text when using HTML formatted labels

不打扰是莪最后的温柔 提交于 2019-12-02 04:47:21

Does anyone know how to control the text color for disabled buttons?

one of the way (you meant Html) is

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class HtmlAndJButton {

    public HtmlAndJButton() {
        final String buttonText = " Whatever, but nothing wise";
        final JButton button = new JButton(buttonText);
        JButton btn1 = new JButton("Toggle");
        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                button.setText("<html><font color=" + (button.isEnabled() ? "blue" : "red") + ">"
                + buttonText + "</font></html>");
                button.setEnabled(!button.isEnabled());
            }
        });
        JFrame f = new JFrame("ButtonTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(2,1));
        f.add(button);
        f.add(btn1);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                HtmlAndJButton t = new HtmlAndJButton();
            }
        });
    }
}

I read the bug report that you posted on your question and they provided a workaround that will solve this issue. Create the below class:

/**
 * Attaches to a JButton to work around Sun bug 4783068.
 * <p>http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4783068</p>
 */
public final class SunBug4783068Fixer implements PropertyChangeListener {

private static final SunBug4783068Fixer INSTANCE = new SunBug4783068Fixer();

public static void attach(AbstractButton to) {
    // Prevents adding it more than once to any single component.
    to.removePropertyChangeListener(INSTANCE);

    to.addPropertyChangeListener(INSTANCE);
}

public static void remove(AbstractButton from) {
    from.removePropertyChangeListener(INSTANCE);
}

public void propertyChange(PropertyChangeEvent evt) {
    if ((evt.getSource() instanceof AbstractButton)
         && "enabled".equals(evt.getPropertyName())) {
        AbstractButton target = (AbstractButton) evt.getSource();
        target.setForeground(target.isEnabled()
            ? (Color) UIManager.getDefaults().get("Button.foreground")
            : (Color) UIManager.getDefaults().get("Button.disabledText"));
    }
  }
}

Then add it to your JToggleButton like so

 final JButton controlButton = new JButton("Toggle Enabled/Disabled");
        controlButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                button.setEnabled(!button.isEnabled());
            }
        });
 controlButton.addPropertyChangeListener(new SunBug4783068Fixer());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!