Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JLabel setIcon() not working...

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?...

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic