Java 7 button text color when using HTML formatted labels

I have a user interface for specific buttons implemented by subclassing MetalButtonUI. Buttons use shortcuts in HTML format. This is a requirement for me, I need to support multi-line button shortcuts.

For some reason, when my application runs on Java 7 (scientifically updated 4, the most current one), the text color with the button disabled is now gray. This does not happen when working on Java 4 or 6.

In HTML for a button label, I can set the font color with. <font color=..>However, this value is ignored when the button is disabled. It seems that somewhere my font color is being redefined when the button is disabled. Use is <body style='color: ..'>also inefficient.

I tried setting Button.disabledText to UIDefaults. This is not what I really want to do, because it affects too many buttons. But in any case, it is not effective for HTML button shortcuts.

MetalButtonUI defines getDisabledTextColor, but its implementation is inefficient.

Similarly, the implementation of the paintText method is inefficient. It is not called for shortcuts in HTML format.

I could override the entire drawing method, but this seems like a too complicated solution.

A patch was fixed in this area, the bug was reportedly fixed in Java 7, see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4783068 The error report is not very clear to me, however. It is unclear what exactly was changed, or how to reverse the new behavior.

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

EDIT: , get-go. paint() . , , button.setForeground(Color.black); Java 6 . Java 7 . mKorbel... !!!!

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.*;


    public class DisabledButtonDemo {


        public DisabledButtonDemo() {
            final JToggleButton button = new JToggleButton(
               "<html><center>Button<br/>Label</center></html>");      

            // Next line sets the text color. 
            // In Java 6 it is respected, for both enabled and disabled state.
            // In Java 7, it is only used for the enabled state.
            button.setForeground(Color.black); 
            button.setPreferredSize(new Dimension(100, 100));

            final JButton controlButton = new JButton(
               "Toggle Enabled/Disabled");
            controlButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    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(controlButton);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }

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

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

№2: Oracle, . http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7176683

+1
2

- , ?

( Html) -

enter image description here

enter image description here

enter image description here

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();
            }
        });
    }
}
+2

, , , . :

/**
 * 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"));
    }
  }
}

JToggleButton

 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());
0

All Articles