pickypg pickypg - 1 year ago 176
Java Question

Changing the background color of a selected JToggleButton

I am trying to change the color of a

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
properties for
at all, or it at least does not supply them itself (there is a quick example online to find all property keys from the
, which in the example is conveniently limited explicitly to

I have tried setting the background color:

Action action = new AbstractAction() {
public void actionPerformed(ActionEvent e) { /* stuff */ }
JToggleButton button = new JToggleButton(action);
// tried with and without opaque true

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:

public void actionPerformed(ActionEvent e)
JToggleButton button = (JToggleButton)e.getSource();
if (button.isSelected()) // alternatively, (Boolean)getValue(Action.SELECTED_KEY)

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)


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

// selected color
g.fillRect(0, 0, w, h);
// selected foreground color
(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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download