Try this one
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < getContentPane().getComponentCount(); i++) {
getContentPane().getComponent(i).setBackground(backgroundColor);
}
}
You are changing the color of JFrame
which is parent of all JPanel
that is added into it. You have to set the background color of all JPanel
added into it.
--EDIT--
As per your last comment - I only want the region where the buttons reside to change color
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
((JButton) e.getSource()).getParent().setBackground(backgroundColor);
}
}