• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mouse click event

 
Seema Sharma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to use JLabel as hyperlink. i have array of JLabels used as hyperlink. I am trying to use mouse click event on these JLabels so that hyperlink opens some window. But the problem is mouse click event what i used is not working here. Can mouse click event be used for arrays. Let me know.


Regards,
Seema
 
Campbell Ritchie
Sheriff
Posts: 51456
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems a rather unusual design, clicking on a label; one would usually associate a button with the URLs and click on the button.

You ought however be able to add a MouseListener to a JLabel and open the URL from its mouseClicked method. If you have an array of URLs then it is probably easy to use a for-each loop to iterate through the array setting up the Listeners and URLs together.
 
Maneesh Godbole
Saloon Keeper
Posts: 11342
16
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:
Hi,
I need to use JLabel as hyperlink. i have array of JLabels used as hyperlink. I am trying to use mouse click event on these JLabels so that hyperlink opens some window. But the problem is mouse click event what i used is not working here. Can mouse click event be used for arrays. Let me know.


You can have one class which extends JLabel. Let us call it MyLabel. In the MyLabel constructor you can define data such as display text, the URL to open. You also add a mouse listener, and the code to open the URL when the mouse click happens.

Now in your frame, you create an array of MyLabel instead of JLabel.
That way each instance of MyLabel will know what to do when it gets the mouse click notification.
 
Maneesh Godbole
Saloon Keeper
Posts: 11342
16
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 need to use JLabel as hyperlink.


Like Campbell rightly pointed out, JLabel is a strange kind of control.
You can make the JButton look and behave like a hyperlink by making use of:
1) JButton supports html as display text. So you can definitely make it display something with a hyperlink look by simply using the <u></u> tag
2) Check out JButton's setContentAreaFilled(boolean isFilled) method
3) Check out JButton's setBorderPainted() method.

Hope this helps.
 
Brian Cole
Author
Ranch Hand
Posts: 912
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Seema Sharma:
I need to use JLabel as hyperlink.


This has come up in the past. Here are some older threads on the topic: one, two, three. You may want to take a look.

i have array of JLabels used as hyperlink. ... But the problem is mouse click event what i used is not working here. Can mouse click event be used for arrays.


Whether or not they are in an array should be irrelevant.
 
Seema Sharma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have implemented mouse click event for JLabel array. Thanks for all your help.
First time its working fine. Next time when this code is called on clicking 'OK' button of other dialog box, it does not work.


In the above code though both e.getComponent() and labelN[nIndex] are containing same values, still "if" condition fails when code is called from other pop-up.
Can you suggest me what could have gone wrong?

Thanks in advance.

Regards,
Seema
 
Darryl Burke
Bartender
Posts: 5149
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm, don't see why .equals(...) wouldn't return true (it should), but here you are actually checking for object identity, so your condition should be


edit Forgot to remove the parens.
[ May 14, 2008: Message edited by: Darryl Burke ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic