• Post Reply Bookmark Topic Watch Topic
  • New Topic

Set color for JLabel

 
Seema Sharma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please let me know how can i set color for JLabel which is used as hyperlink.

Regards,
Seema
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Seema Sharma:

Please let me know how can i set color for JLabel which is used as hyperlink.


Which color are you talking about? Background? Foreground? Have you read the API? Did you find something there?
 
Seema Sharma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maneesh,
For JLabel which is used as hyperlink, i have used the below code:


I have observed that now color is changing using the above code but mouse event functionality is not working. That is, this JLabel is not clickable.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Seema Sharma:
... this JLabel is not clickable.


Yes that is correct. In case you want to make it "clickable", you will have to add a mouse listener, trap the clicks yourself and process it accordingly.
Though this technically would work, I personally would recommend you use JButton instead of a JLabel.
To make it look and behave like a hyperlink, you will have to hide the borders and make it opaque (setContentAreaFilled(false) is the recommended way).
 
Seema Sharma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have already have added mouse listener to the label still click event is not taking place.
But when i don't add color code and click on the hyperlink its working. This is what works:


Any there any other way to set color for hyperlink used as JLabel?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Seema Sharma:

I have already have added mouse listener to the label still click event is not taking place.


Can you post your mouse listener code so we can try and figure out why it is not working?

Originally posted by Seema Sharma:

Any there any other way to set color for hyperlink used as JLabel?


 
Ted Smyth
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why reinvent the wheel?

Use SwingX's hyperlink component.

http://swinglabs.org/downloads.jsp
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ted Smyth:
Why reinvent the wheel?


True. But if I was halfway in designing my wheel and was stuck at some point, I would certainly like to find out why my wheel is not working. I might learn that I have forgotten to add the spokes and this knowledge would certainly stay with me and help me in the future.

Needless to say, this is my personal opinion and others have a right to think otherwise and I respect that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!