aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How do you add image in a JLabel? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do you add image in a JLabel?" Watch "How do you add image in a JLabel?" New topic
Author

How do you add image in a JLabel?

Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

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?


Repeal Obamacare.
http://www.dontfundobamacare.com/
http://liberty-amendments.freeforums.net/
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Jan 22, 2011
Posts: 48

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

Joined: Jun 13, 2009
Posts: 2226
    
    8
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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Jan 22, 2011
Posts: 48

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

Joined: Jan 22, 2011
Posts: 48



What do I do?
Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

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

Joined: Jul 26, 2007
Posts: 10535
    
    9

Did you view the link Rob provided?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

edit: Sorry, I hadn't noticed the link in Rob's post and had posted the same link


luck, db
There are no new questions, but there may be new answers.
Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

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

Joined: May 03, 2008
Posts: 4664
    
    5

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

Joined: Mar 14, 2011
Posts: 3

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

Joined: Oct 27, 2005
Posts: 19783
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you add image in a JLabel?