This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JLabel setIcon() not working... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JLabel setIcon() not working..." Watch "JLabel setIcon() not working..." New topic
Author

JLabel setIcon() not working...

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
I am creating an image button class using JLabel and it's setIcon() method.
It also has a MouseListener, so that when the mouse goes over the button, the image changes colour.

I had it working nicely on my PC, buy when I uploaded my applet and tried it, no image would be displayed on m0useover...

It now displays no images at all regardles of where the mouse is. The code is quite simple, but I can't work out what is missing or what might be wrong. Does anyone have any ideas?...

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you've overridden paintComponent() and commented out the super line
public void paintComponent(Graphics g) {
//super.paintComponent(g);

It worked OK for me (uncommenting above). I also changed the name from Button,
to avoid any conflicts with java.awt.Button.

only problem was the text displayed over the icon, so I passed "" as the text
in the constructor, and the icons toggled between #1 and #2, depending on mouse position
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Cheers Michael,

Meant to comment out the entire paintComponent method... late night.

I've found the source of the problem now. The filenames of the images I were using were 'yellowButton.jpg' and 'blueButton.JPG' . My code had used 'yellowButton.jpg' and 'blueButton.jpg' . The JPG / jpg difference was the reason it wasn't working when my applet was on the server, despite the image not coming up as null and despite the applet working when it was local on my machine...

Anyhow, thanks for checking the code out for me and your help. Much appreciated.

Cheers,
James
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JLabel setIcon() not working...
 
Similar Threads
How can I display an image on an AWT panel
Magnifying only a part of the image
Picture Woes
Help with Scrollable Image please...
getImageIcon() : how does it work?