Search code examples
javaimageswingembedded-resourceimageicon

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.


Solution

  • //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 /.