Change JButton's Disabled Foreground (Font) Color for Windows

user2323030 picture user2323030 · May 1, 2013 · Viewed 9.4k times · Source

I realize this sounds a LOT like many of the questions already posted out there, but please read on; this has a seemingly similar problem but has not worked with many solutions already provided.

I am currently writing in Java with Eclipse on both OS X and Windows. With OS X, I have a JButton that goes from .setEnabled(true) to .setEnabled(false). When this happens, as it gets disabled, I change its background color via .setBackground(someColor). The entire time this is happening, the foreground (font) color does not change and stays black. This is what I want, it's perfect the way it is.

Then comes the problem with Windows. Same as above, I have a JButton that goes from .setEnabled(true) to .setEnabled(false). I also change its background via .setBackground(someColor). However, when this happens, the foreground (font) color does not stay constant – it changes from black to a light gray. This is extremely inconvenient and makes it very hard to read with the new background color.

So the question is: How do I change the foreground color of a disabled JButton?

I have already tried the following:

button.setForeground(Color.BLACK);

button.setText(<html><font color = black>BUTTON</font></html>);

UIManager.put("Button.disabledText", Color.BLACK);

UIManager.getDefaults().put("Button.disabledText", Color.BLACK);

UIManager.put("Button.foreground", Color.BLACK);

UIManager.getDefaults().put("Button.foreground", Color.BLACK);

None of it worked. And I've also tried the following:

  • Exporting the OS X to a .jar file, then using it in Windows. Windows font color still changes.
  • Compiling an .app file for OS X and an .exe file for Windows. The problem still remains.

Is there ANY other solution out there that I've overlooked?

At the moment, I've resorted to leaving the font as the unsightly gray it currently is and changing the background (which, for some reason, can still easily be changed via .setBackground()) to other colors that can accommodate it.

So why does there seem to be this color difference between OS X and Windows? I prefer the OS X color scheme, but I don't really want to have 2 sets of code for what is essentially the same program.

What should I do?

Answer

mKorbel picture mKorbel · May 1, 2013

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 {

    final String buttonText = " Whatever words, <br> but nothing wise";
    final String buttonText1 = " Whatever words, <br> but nothing wise, "
            + "<br> plus 1st. line, ";
    final String buttonText2 = " Whatever words, <br> but nothing wise, "
            + "<br> plus 1st. line, <br> plus 2nd. line,";
    private JButton btn1 = new JButton("Toggle");
    private JButton button = new JButton(buttonText);
    private JButton button1 = new JButton("Toggle");
    private JButton button2 = new JButton("Toggle");

    public HtmlAndJButton() {
        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());
                button1.setText("<html><font color=" + (button1.isEnabled()
                        ? "red" : "green") + ">" + buttonText1 + "</font></html>");
                button1.setEnabled(!button1.isEnabled());
                button2.setText("<html><font color=" + (button2.isEnabled()
                        ? "green" : "yellow") + ">" + buttonText2 + "</font></html>");
                button2.setEnabled(!button2.isEnabled());
            }
        });
        button.setText("<html><font color=red>" + buttonText + "</font></html>");
        button1.setText("<html><font color=green>" + buttonText1 + "</font></html>");
        button2.setText("<html><font color=yellow>" + buttonText2 + "</font></html>");
        JFrame f = new JFrame("ButtonTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(2, 2));
        f.add(button);
        f.add(button1);
        f.add(button2);
        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();
            }
        });
    }
}