• Post Reply Bookmark Topic Watch Topic
  • New Topic

JLabel icons  RSS feed

 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some simple code that instantiates a JLabel on a JPanel. What I want to do is just have an icon in the JLabel. However, doing


shows nothing.

If I change it to be:


I then see a text JLabel.

If I change it to:



I see nothing again.
This is very disappointing in addition to being baffling. If the "bmw.jpg" icon cannot be found I would expect to get an exception. If Java isn't throwing an exception I would expect to see the text portion of the JLabel that is both text and icon.
This code is from JustJava2 Chapter 21 - JPanel.
[ December 02, 2008: Message edited by: Red Smith ]
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Red Smith:
This is very disappointing in addition to being baffling. If the "bmw.jpg" icon cannot be found I would expect to get an exception. If Java isn't throwing an exception I would expect to see the text portion of the JLabel that is both text and icon.


Do you start seeing the Icon if you minimize and maximize the frame?
If yes, the label is being displayed before the Image is fully loaded.
In that case, check out MediaTracker classs.

More related to Swing. Moving.
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
duplicate post
[ December 02, 2008: Message edited by: Red Smith ]
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Maneesh. You are on to something. First, here is the rest of the code for the small application:



So the JFrame is initially 400 x 100. If I pull on the right side and make it wider (approx 700 x 100) I eventually do see the JLabel with the Icon in the upper left corner of the JPanel (which seems to appear as a smaller white rectangle inside the JFrame). I thought I could fix this by doing:



but it still inexplicably shows up in the upper left corner???
[ December 02, 2008: Message edited by: Red Smith ]
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Red Smith:

but it still inexplicably shows up in the upper left corner???


Take out this line
to get things working as required.
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:


Take out this line
to get things working as required.


Thanks again for replying. I still had my same problem with that line removed, but I found out why. I made what I thought was a small icon in Windows Paintbrush. However, what I had was a large image that only had color in the upper left. So the JLabel with the icon is huge and centered in the JPanel/JFrame, which is too small to show the upper left colored part. Also the "icon" was so large that it shoved the text in the text/icon version way to the right, making it disappear beyond the right edge of the JFrame. I downloaded an icon that was 16 x 16 and the code worked.
[ December 02, 2008: Message edited by: Red Smith ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!