• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JLabel changes icon when clicked!

 
Maron Balinas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 imageIcon for one JLabel and I just did that when I clicked it the imageIcon will change, but I just what to do that the imageIcon will change vice versa when you click it repeatedly. Is it possible that the JLabel first imageIcon alternating to the second imageIcon when you clicked?

I'm doing it for switch image that turns on and off and I coded it in class that extends JLabel.

Thanks for help in advance and sorry for my explanation and English.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of reinventing the wheel, I recommend you use a JToggleButton. You can set the icon and the pressedIcon, and if you want the appearance of a label, you can set whether the border is painted and whether the contentArea is filled.
 
Piet Souris
Ranch Hand
Posts: 1152
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maron,

if you DO want to use a JLabel, then simply add a MouseListener to this label. In the ML-code, every time the label is clicked, change the icon of the label. In this way, you can use as many icons as you like.

Greetz,
Piet
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic