aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Using an image (jpg) 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 "Using an image (jpg) in a JLabel" Watch "Using an image (jpg) in a JLabel" New topic
Author

Using an image (jpg) in a JLabel

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I am trying to place an image as a JLabel into my JFrame. The spacing in the JFrame indicates that the space for the image is there but the image just does not get displayed. There is no exception thrown.

Can someone see what I am doing wrong? TIA
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Since your code reveals that you are using a null layout and setBounds(...)how do you come to know this?
the space for the image is there


I suggest you learn to follow the Java coding conventions: variable names should start with a lowercase letter.

To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.


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

Joined: Feb 03, 2012
Posts: 217
Thanks for the reply. I know this because there are other JLabels in the JFrame and there is a gap of the expected size among the other JLabels. If I comment out the errant JLabel the gap goes away. I will try to reproduce the problem in an SSCCE and post it.

P.S. I am aware of the programming standards but this was one of the first things I wrote in Java and never got around to fixing it. Until now.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Here is a sanitized SSCCE that does indeed reproduce the problem.


I don't know if the image itself is the problem but since it is not available via a URL, I don't know how to upload it or if that is even necessary. My guess is that any image would yield the same result. At least it does for me.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

You're still using a null layout and setting the bounds of each component. That invalidates the assumption that
the space for the image is there

Try removing the call to setLayout(null) and all setBounds(...), set a FlowLayout and set a LineBorder on each label and see what happens then.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Thanks again for the reply. I am not sure I completely understood what you were asking me to do but here is what I did:

1) Removed all setBounds.
2) Changed the layout manager from null to FlowLayout:

The result was a panel that initially only showed the title bar. I had to resize it to see what was in it. I am guessing that you did not mean for me to remove the setBounds on the JFrame just on all the JLabels. The result was still no image and the other JLabels were run together (not unexpected). I'm not concerned with that part as I can adjust it all once I get the image showing up. In this case however, there was no "extra" space where the image should have been, FWIW.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
After playing with this a bit more to no avail, I am wondering if I have the file path correct. Where files are relative to the class file is still a little mysterious to me. The jpg is in the same directory as the class referencing it. Thus I don't specify anything other than the file name. Is that correct? Would I get an exception if the file was not found? If not, is there a way to verify the file path?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

This should help: Loading Images Using getResource.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Thanks for the suggestion but it did not help other than to more definitively demonstrate where the problem lies. Here is what I have now:

Neither alternate JLabel is displayed so clearly it is executing the JLabel that inserts the image so it must be finding the image file. Unfortunately, the image is still not being displayed.
m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 157


- (Image)Icon doesnot returns any exception by default

- have to test if logoURL is null or not

- if not the you have to flush() for (Image)Icon before add to JLabel
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Isn't that what I did?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using an image (jpg) in a JLabel