aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes loading image in panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "loading image in panel" Watch "loading image in panel" New topic
Author

loading image in panel

craig peddie
Greenhorn

Joined: May 15, 2013
Posts: 12
I've followed the thread to create a panel with a background image and everythign is working except loading the image.


The getResource calls work fine in both cases and return the proper path for the png I'm trying to load- but, the beginning of the URL string contains "file:" before the first slash. The imageIO.read call always returns null, and I'm suspecting it's because of the file: tag at the beginning of the URL. What is the proper way I should be finding the resource and passing the string to imageIO?

Thanks for any help!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
How to Use Icons shows how to read a file and use an Icon to display the image using a JLabel.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 172
    
    1

To insert a image in the background of JPanel, clickhere.

But you can do this using JLabel too. It's easy than this..


Ramesh-X
craig peddie
Greenhorn

Joined: May 15, 2013
Posts: 12
Thanks- that is exactly the JPanel code I am using. My problem though is that the imageIO.read call is not working. The url string has "file://.....pathname" and the read call always returns null as though it can't find the file. I'm suspecting it is because of the "file:" tag prepended to the path. That actual pathname is correct and the image file is indeed there.

What I'm trying to layout is a GUI that has a panel across the top of the frame that contains an image for the background, and then I want to paint other transparent images on top of this background that will act as buttons.

Thanks....
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 172
    
    1

Try this. I always use this code when I want an Image from URL.



ImageIcon class is in javax.swing package.

"path" is the path of the image relative to the default package.

eg: If your image is in inside "project.resources.images" package, the path will be "project/resource/images/icon.jpg"
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: loading image in panel