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.
Simon, I think you can add an ActionEvent by calling addxxxListener(...).
Paul Stevens
Ranch Hand
Joined: May 17, 2001
Posts: 2823
posted
0
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
posted
0
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)
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
posted
0
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
posted
0
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).]