How to change the color of a button?

0

I'm doing some experiments to get a better understanding of Netbeans IDE and I had this doubt about the color of a button.

By right-clicking on any jButton and selecting Properties, I set the background item to green, for example, but only the border of the button is selected.


JButtonbeforechangingProperties


JButton after changing Properties

I have already changed LAF for Windows and returned to Nimbus (default) and nothing different happens.

Is it possible to change the fill color of a button?

    
asked by anonymous 31.12.2017 / 15:45

1 answer

1

To change the color, just use setBackground() by passing the desired color through the class Color , using their constants or creating custom colors:

botao.setBackground(Color.red);

However, most LookAndFeels (LAF) already have default color settings and even changing the above method, does not allow to change correctly in some cases. To resolve this, you need to "remove" this styling of the component, using the methods below:

    botao.setContentAreaFilled(false);
    botao.setOpaque(true);
    botao.setBackground(Color.RED);

Result:

  

Note: In doing so, you are giving up all LAF styling, not just background and foreground colors.

    
31.12.2017 / 15:56