Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the background image of a button in java swing

Tags:

java

swing

I want to change the background image of a JButton in swing,
The method : Button.setIcon (),
Set’s the icon only not the background, Is there a easy way to do that?

like image 569
java-love Avatar asked Jan 21 '14 22:01

java-love


3 Answers

You can still use setIcon but you will need to set the alignment to make the text appear over the image

button.setHorizontalTextPosition(SwingConstants.CENTER);
like image 131
Reimeus Avatar answered Sep 29 '22 10:09

Reimeus


One option is to subclass JButton, override paintComponent() method and paint the icon there.

like image 31
Jakub Zaverka Avatar answered Sep 27 '22 10:09

Jakub Zaverka


The easy way use SynthLookAndFeel as described here or you could go with creating a class that extends JButton and provide a new implementation of paintComponent(Graphics g)

like image 22
Alex Goja Avatar answered Sep 27 '22 10:09

Alex Goja