aspose file tools*
The moose likes Java in General and the fly likes Putting an 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 » Java in General
Bookmark "Putting an image in a JLabel" Watch "Putting an image in a JLabel" New topic
Author

Putting an image in a JLabel

Kasun Liyanage
Greenhorn

Joined: Mar 09, 2011
Posts: 23

Hi!

Ok, i have a JLabel on my app window, and i wanna load an image that resides on my harddisk or on the net into this already existing JLabel. i tried to do it like this from inside the main method by it gives me an error.


(jLabel1 label exists inside the NewJFrame frame)



How to fix this?
Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

Then that code must be in a static method of your class. We don't know what your class is called but let's suppose it's called "Test". Then do this:


Alternatively, restructure your code so that you aren't using a static method.
Kasun Liyanage
Greenhorn

Joined: Mar 09, 2011
Posts: 23

Thanks.

If i'm not using getResource(), what other ways are there to load an image (directly from harddisk/the net) to an already existing JLabel?



Appreciate if anyone could post some codes showing that.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

Any way which provides a URL which points to the image. If the image is on the net (and if it's accessible) then you'll have a URL pointing to it. If it's on the disk, then you can produce a File object which refers to it, and File has a method which produces a URL pointing to the file.

You would use the getResource() method if you knew the image was in your classpath somewhere (e.g. in a jar in the classpath).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Putting an image in a JLabel