Chase
Chase

Reputation: 25

How to reference a local image in java?

jLabel5.setIcon(new javax.swing.ImageIcon("./i/login.png"));

I'm trying to reference that image. The path is correct, and the image actually exists. When I use the full path (I.E. "C:/ blah blah" it works, but this doesn't?

The image folder is in the bin folder.

Upvotes: 1

Views: 8300

Answers (1)

Azad
Azad

Reputation: 5055

//This will retuns the URL of the image file inside your project
  this.getClass().getResource("/i/login.png");

So, your code will be :

URL imageUrl = this.getClass().getResource("/i/login.png");
jLabel5.setIcon(new javax.swing.ImageIcon(imageUrl));

If the image is outside your current package, start the path with /i/login.png, else, don't need the /.

Upvotes: 4

Related Questions