wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Capture Hyperlink click in JButton or JLabel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Capture Hyperlink click in JButton or JLabel" Watch "Capture Hyperlink click in JButton or JLabel" New topic
Author

Capture Hyperlink click in JButton or JLabel

Jes Sie
Ranch Hand

Joined: Jul 24, 2001
Posts: 188
HI guys,
In the previous post at http://www.javaranch.com/ubb/Forum2/HTML/002607.html , a kind soul has donated some codes in regards to my previous questions. The code goes like this: -
JLabel MyLink = new JLabel("Link Text");
MyLink.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) { // Do whatever you want to do here
}}
That's cool, but how can I capture the Hyperlink in JLabel, example, JLabel MyLink = new JLabel("<html>A HTTP Link");
How could I add addHyperlinkListener to JLabel or JButton so that I could capture the URL upon clicking?
Any hint would be appreciated.

------------------
Thank you.
- Simon See
simon-ivy@usa.net
-after office hours-
necro-mani-cide


Thank you.<br /> <br />- Jess Sie<br /> <a href="mailto:jess@see-consulting.com" rel="nofollow">jess@see-consulting.com</a>
Jeff Chang
Greenhorn

Joined: Aug 23, 2001
Posts: 23
Simon,
I think you can add an ActionEvent by calling addxxxListener(...).
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Are you saying you want to treat a JLabel as a hyperlink? Where is the URL that you are trying to capture? What exactly do want to happen?
Jes Sie
Ranch Hand

Joined: Jul 24, 2001
Posts: 188
Originally posted by Paul Stevens:
Are you saying you want to treat a JLabel as a hyperlink? Where is the URL that you are trying to capture?

Yes. Is a "internal URL". Basically this URl has got some ID like "http://123".
Originally posted by Paul Stevens:

What exactly do want to happen?

Basically want to have the even to capture the click...simple but then how can I have it to read the URL like above? (I know this is a piece of cake with JEditorPane.Hyperlinklistener...making a simple browser. But that's not what I'm doing)


------------------
Thank you.
- Simon See
simon-ivy@usa.net
-after office hours-
necro-mani-cide
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
If you just want the String representing the URL, getText() will return it for you. If you want the click event, you were already given that code.
Jes Sie
Ranch Hand

Joined: Jul 24, 2001
Posts: 188
Originally posted by Paul Stevens:
If you just want the String representing the URL, getText() will return it for you. If you want the click event, you were already given that code.

Thanks for following up on this Paul.
Yeah, the getText does do some of the tricks, but how about just grabbing the URl as opposed to grabbing the whole HTML text. U know like the HyperlinkLIstener which has got this event.getURL(). Got any more ideas there?
Matt Hansen
Ranch Hand

Joined: Jul 31, 2001
Posts: 34
If you want the JLabel to store the url and display some different text. i.e. it says 'My Link' and clicking on it takes you to http://www.somewebpage.com You probably need to implement your own JLabel which has a url field and a getURL method.
[This message has been edited by Matt Hansen (edited September 28, 2001).]
 
GeeCON Prague 2014
 
subject: Capture Hyperlink click in JButton or JLabel