• Post Reply Bookmark Topic Watch Topic
  • New Topic

JLabel  RSS feed

 
Rocky Summers
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
is there any way i can add sort of an actionlistener to a jlabel just like a jbutton? because i want it so that when user clicks on a jlabel, it sorts of does things i want it to do.
thanks,
rock
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A label that does stuff when you click on it == a button. ie. why don't you just use a JButton instead of a JLabel ?
And no, I don't think you can add actionListeners to JLabels
 
Lalit K Kumar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that by any way you can add "addactionlistener" to a jlabel.
Instead you can use jButton.
 
Rocky Summers
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh... okay... thank you guys.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could add a MouseListener to the JLabel and use the mousePressed() method like the actionPerformed() method. However, there's really no reason not to use a JButton for this...
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a custom class that I call ActiveURL that does exactly this. It's basically a JLabel with a mouseListener attached. When you point at it, the cursor changes to a hand and the text changes color. When you click on it a browser launches to the page specified in the constructor. It looks and acts just like a webpage link. I keep meaning to post the source code for it on my Freeware Java Classes page (http://filenabber.com/free-java-classes/) but keep forgetting. If anyone wants to see it, let me know and I can post the code.
Brian
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nathan Pruett:
You could add a MouseListener to the JLabel and use the mousePressed() method like the actionPerformed() method. However, there's really no reason not to use a JButton for this...

Agreed, and if you remove the border,
button.setBorderPainted(false);
and remove the focus
button.setFocusPainted(false);
Then all you really have is a JLabel. And all the cool clickity click stuff is already there.
 
Rocky Summers
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANK YOU ALLLL!!
And Brian, I'd like to see how that code of yours works! Can you please post it up.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!