How to set jbutton color

How to set JButton color?

I tried this:

button.setBackground(Color.red);

but without success. It just changes the color of the button border. Then I tried to overridepaintComponents

public void paintComponent(Graphics g) {
   g.setColor(Color.GREEN);
   g.fillRect(0, 0, getSize().width, getSize().height);
}

but now i don't see text in jbutton

+5
source share
2 answers

The best way to colorize your buttons is to use ImageIcons instead of text. You can use Gimp to develop them.

Make sure the background is transparent !

button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/myimage.png")));

This is the disabled button button.setDisabledIcon(...:

enter image description here

This button is on, not pressed:

enter image description here

This is the button with the button pressed:

enter image description here

Changing the background color after clicking is done using Swing. For this you need only 2 images.

+6

, . . http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/index.html

, , . , . LAF , :

button.setBackground(color);
button.setBorder(null);
+3

All Articles