javaswingcustom-componentuimanagerjtogglebutton

Changing the background color of a selected JToggleButton


I am trying to change the color of a JToggleButton when it has been selected in a reliable, look and feel independent way.

If using the Metal L&F, then using the UIManager is an approach:

UIManager.put("ToggleButton.selected", Color.RED);

Note: Iyy pointed out that I had a typo in the property name above, but I will leave it above for people getting here, but the actual property name is supposed to be:

UIManager.put("ToggleButton.select", Color.RED);

However, this does not work in my current Look and Feel (currently Windows XP). After some further analysis, it appears that the system look and feel in Windows (still XP) does not use any of the Color-based UIManager properties for ToggleButton at all, or it at least does not supply them itself (there is a quick example online to find all property keys from the UIManager, which in the example is conveniently limited explicitly to Color properties).

I have tried setting the background color:

Action action = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) { /* stuff */ }
};
JToggleButton button = new JToggleButton(action);
// tried with and without opaque true
button.setOpaque(true);
button.setBackground(Color.RED);

Not only does it not change the selected state, but that does not even effect the unselected state.

I have tried changing the background color only after receiving the action:

@Override
public void actionPerformed(ActionEvent e)
{
    JToggleButton button = (JToggleButton)e.getSource();
    if (button.isSelected()) // alternatively, (Boolean)getValue(Action.SELECTED_KEY)
    {
        button.setBackground(Color.RED);
    }
}

None of that works. The only thing that I have found to work requires me to draw the button myself in the selected state (which leads to a working example, albeit non-standard looking):

private class ColoredToggleButton extends JToggleButton
{
    ColoredToggleButton(Action action, Color color)
    {
        super(action);

        setBackground(color);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if (this.isSelected())
        {
            int w = getWidth();
            int h = getHeight();
            String s = getText();

            // selected color
            g.setColor(getBackground());
            g.fillRect(0, 0, w, h);
            // selected foreground color
            g.setColor(SystemColor.controlText);
            g.drawString(s,
                         (w - g.getFontMetrics().stringWidth(s)) / 2 + 1,
                         (h + g.getFontMetrics().getAscent()) / 2 - 1);
        }
    }
}

That is slightly modified from a comment in this Java bug report. Interestingly (amusingly?), in claims to have been fixed in 1998.

Does anyone know of a better, L&F independent way to set the background color of a selected JToggleButton?


Solution

  • You might see if setIcon() is sufficient for your purpose, but you can also override paint() in the ButtonUI delegate.

    Addendum: @kleopatra's comment is well-taken: changing the UI delegate is not trivial. @mKorbel's recent example shows both the difficulty and versatility of the approach. Its essential advantage is look & feel independence.

    Some less ambitious approaches are mentioned here.