Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: using an image as a button

I would like to use an image as a button in Java, and I tried to do this:

BufferedImage buttonIcon = ImageIO.read(new File("buttonIconPath")); button = new JButton(new ImageIcon(buttonIcon)); 

But this still shows the actual button behind the image, I would only like the image to function as the button, how can I do this?

like image 333
3sdmx Avatar asked Feb 04 '11 13:02

3sdmx


People also ask

What is JLabel Java Swing?

JLabel is a class of java Swing . JLabel is used to display a short string or an image icon. JLabel can display text, image or both . JLabel is only a display of text or image and it cannot get focus . JLabel is inactive to input events such a mouse focus or keyboard focus.

What is Java Swing package?

Java Swing is a lightweight Java graphical user interface (GUI) widget toolkit that includes a rich set of widgets. It is part of the Java Foundation Classes (JFC) and includes several packages for developing rich desktop applications in Java.


1 Answers

Remove the border like so:

button.setBorder(BorderFactory.createEmptyBorder()); 

and then also the contents1:

button.setContentAreaFilled(false); 

1: Taken from the solution added to the question by @3sdmx

like image 149
jzd Avatar answered Sep 22 '22 12:09

jzd