views:

61

answers:

2

I want the button on click it never change the background color, by default the color will change to light blue grey color.

So this is my code

JButton b = new JButton();
b.setBackground(SystemColor.control);

I want the button when on click it will never change the background color.

+1  A: 

You could modify the colors that is used for the background and foreground. How you do it depends on what Look And Feel you are using.

If you are using Nimbus, there is a list of colors here, and here is an article about how you change the colors. And here is another article that can be helpful.

Jonas
+1  A: 

The painting of the button depends on LAF. You can do one of the following:

  1. Define your own UI delegate and assign it to the button. Disadvantages: complex, breaks LAF.
  2. Extend JButton and implement paintComponent(). Disadvantages: You will need to paint button's label yourself.
  3. Remove the button's background painting altogether by invoking setContentAreaFilled(false) and then add the button in the opaque panel with the needed background color. Advantages: button is unchanged, LAF isn't broken. Disadvantages: you need to create a panel for each button, and adjust it to fit the button exactly, which can be quite difficult, depending on the layout.
Taisin