• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Capture Hyperlink click in JButton or JLabel

 
Jes Sie
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Chang
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon,
I think you can add an ActionEvent by calling addxxxListener(...).
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic