Changing the background color of a selected JToggleButton
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.
"ToggleButton.selected" is wrong, it require "ToggleButton.select". And should be update to the component.
UIManager.put("ToggleButton.select", Color.WHITE);
SwingUtilities.updateComponentTreeUI(togglebuttonname);
JToggleButton btn = new JToggleButton(...);
btn.setUI(new MetalToggleButtonUI() {
@Override
protected Color getSelectColor() {
return Color.RED;
}
});
When it using "Windows look and feel"; in Netbeans you have to do only two things.
- Go to properties
- Un-select 'contentAreaFilled'
- Select 'opaque'
- You can set a background color in properties or by hard coding
In other way,
jToggleButton.setContentAreaFilled(false);
jToggleButton.setOpaque(true);
jToggleButton.setBackground(Color.red); //Your color here
That's all.:-)