1

How to add a picture to JLabelin Java?

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
Rasith Upamal
  • 11
  • 1
  • 3

4 Answers4

0

You can use ImageIcon

JLabel l = new JLabel(new ImageIcon("path-to-file"));
dryairship
  • 6,022
  • 4
  • 28
  • 54
0

Try this code:

ImageIcon imageIcon = new ImageIcon("yourFilepth");
JLabel label = new JLabel(imageIcon);

For more Info

VVN
  • 1,607
  • 2
  • 16
  • 25
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