java-love
java-love

Reputation: 515

How to set the background image of a button in 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?

Upvotes: 5

Views: 9602

Answers (3)

Reimeus
Reimeus

Reputation: 159754

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);

Upvotes: 5

Alex Goja
Alex Goja

Reputation: 548

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)

Upvotes: 3

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

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

Upvotes: 4

Related Questions