File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JLabel setIcon() not working..." Watch "JLabel setIcon() not working..." New topic

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) {

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.

I agree. Here's the link:
subject: JLabel setIcon() not working...
It's not a secret anymore!