I am developing a Java Desktop Application. In it I have 4 JButtons
on a JPanel
. Now I want that whenever a button is clicked its background color
just use null
to use the default color:
button1.setBackground(Color.ORANGE);
button2.setBackground(null);
...
consider using JToggleButtons with a ButtonGroup, set the Icon and PressedIcon of the buttons. No need to change the background color.
button1 = new JToggleButton(new ImageIcon("0.jpg"));
button1.setSelectedIcon(new ImageIcon("1.jpg"));
button2 = new JToggleButton(new ImageIcon("0.jpg"));
button2.setSelectedIcon(new ImageIcon("2.jpg"));
...
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
...