Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: how to add image to Jlabel?

Tags:

java

image

jlabel

Image image = GenerateImage.toImage(true); //this generates an image file
JLabel thumb = new JLabel();
thumb.setIcon(image)
like image 850
KJW Avatar asked Sep 05 '25 12:09

KJW


1 Answers

You have to supply to the JLabel an Icon implementation (i.e ImageIcon). You can do it trough the setIcon method, as in your question, or through the JLabel constructor:

Image image=GenerateImage.toImage(true);  //this generates an image file
ImageIcon icon = new ImageIcon(image); 
JLabel thumb = new JLabel();
thumb.setIcon(icon);

I recommend you to read the Javadoc for JLabel, Icon, and ImageIcon. Also, you can check the How to Use Labels Tutorial, for more information.

like image 200
Tomas Narros Avatar answered Sep 09 '25 05:09

Tomas Narros