• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you add image in a JLabel?

 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought something like this would be enough. I even put the image in the same folder as the Eclipse project. However...I think I needed the absolute path. Also, it's just occurred to me that I have no clue how it'd find that Image file if I turned the program into a jar file and sent it to another computer. Any ideas?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you want to use is the method getResource() in the Class class. That looks for resources relative to where the class was loaded from. So, it will work just the same if your application is a bunch of .class files on the file system or packaged up in a Jar. For example:
will load an image that is in the same folder (in a Jar or otherwise) as the .class file of the current class. You can start the filename with "/" if you want a path relative to the the root of the Jar.

Note that ImageIcon has a constructor that takes a URL object as argument, which is what getResource() returns.
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought a URL was a for webpages?

Also, how would you ship an image with a jar?


Also, is there a way to find a file or something if it's not right there? I mean...Windows search can find stuff hiding in the crevices of my computer hard drive.


 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean...Windows search can find stuff hiding in the crevices of my computer hard drive.


Yes and it will take a minute or two to do this. This is not acceptable response time, so you should put your image where the program can find it.

Read the section from the Swing tutorial on How to Use Icons for more information.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Adcock wrote: I thought a URL was a for webpages?

Also, how would you ship an image with a jar?

URLs are for locating a number of things. Web pages, files...and files within jars.

A jar is just a zip file, so sticking other resources in it isn't a problem. With most IDEs, if you put the image in same directory as your source (.java) file it'll automatically get packaged into the jar along with the .class file.
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Paul Adcock wrote: I thought a URL was a for webpages?

Also, how would you ship an image with a jar?

URLs are for locating a number of things. Web pages, files...and files within jars.

A jar is just a zip file, so sticking other resources in it isn't a problem. With most IDEs, if you put the image in same directory as your source (.java) file it'll automatically get packaged into the jar along with the .class file.


I got it to work without the getResources thing. However, with it...it says I have a Null Pointer Exception. I saved the image inside the package that the class is part of. I tried saving to the .class file location but it said I couldn't save there. Where do I save it so it won't return null?

 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What do I do?
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to know how to get the image to come with the class if I put it in a jar file and how exactly to get the getClass.getResources() to find it. So far I've been unsuccessful.



 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you view the link Rob provided?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
edit: Sorry, I hadn't noticed the link in Rob's post and had posted the same link
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say, you wouldn't happen to be the same Darryl Burke from the Java Programming Forums would you?

I'll look at the link.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Adcock wrote:Say, you wouldn't happen to be the same Darryl Burke from the Java Programming Forums would you?

Is there any other?

I'll look at the link.

Yes, please do. Then if there's anything you still don't understand, you will be in a position to ask a much more specific question.
 
Kumar Karunakaran
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the sample program which displays an Image on a JLabel. This program Login.java is saved ina folder in E:\Inventory. So the full path of the program is E:\Inventory\Login.java. The image L3.jpg is in the location E:\Inventory\images. That is, the image L3.jpg is in th folder images. Now I hope you know what to do. Execute the code and enjoy!!

 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using resources as in Matthew Brown's post is a lot better. That "." in your path is the current directory, where the application was started from. That may not be the root of the application, or the folder of the JAR file the application is in. Your code would then be broken.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic