How to add a picture to JLabel
in Java?
Asked
Active
Viewed 7,338 times
1

ParkerHalo
- 4,341
- 9
- 29
- 51

Rasith Upamal
- 11
- 1
- 3
-
1there are other elements for picture why you want to use label for that? – Vishrant Feb 18 '16 at 11:50
4 Answers
0
You can use ImageIcon
JLabel l = new JLabel(new ImageIcon("path-to-file"));

dryairship
- 6,022
- 4
- 28
- 54
0
jLabel1 = new javax.swing.JLabel();
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\admin\\Desktop\\Picture 34029.jpg"));
Here i posted the code that got generated automatically in netbeans.Hope my code helps in this regards keep coding goodluck.

SmashCode
- 741
- 1
- 8
- 14
0
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.
Also have a look at this tutorial: Handling Images in a Java GUI Application

Jad Chahine
- 6,849
- 8
- 37
- 59