aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Mouse click event Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Mouse click event" Watch "Mouse click event" New topic
Author

Mouse click event

Seema Sharma
Ranch Hand

Joined: May 24, 2007
Posts: 78
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Jul 26, 2007
Posts: 10451
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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

Joined: Sep 20, 2005
Posts: 862
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.


bitguru blog
Seema Sharma
Ranch Hand

Joined: May 24, 2007
Posts: 78
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

Joined: May 03, 2008
Posts: 4642
    
    5

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 ]

luck, db
There are no new questions, but there may be new answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mouse click event