*
The moose likes Swing / AWT / SWT and the fly likes Making clickable links using HTML in a JLabel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Making clickable links using HTML in a JLabel" Watch "Making clickable links using HTML in a JLabel" New topic
Author

Making clickable links using HTML in a JLabel

Kerry Shetline
Greenhorn

Joined: Feb 09, 2006
Posts: 14
It's easy enough to turn an entire JLabel into a clickable link for one URL or action. That's not what I'm looking for here.

What I'm wondering about is this: The simple HTML 2.0 parsing Java does allows you to use the <A...></A> markup to make things that look just like clickable links... they're blue, they're underlined. But I see no mechanism to detect clicks on these links so that it's possible to respond to them.

Suppose I do something like this:

someJLabel.setText("<html>The rain in <a href="action.spain">Spain</a> falls mainly in the <a href="action.plain">plain</a>.");

That should produce a JLabel that looks like this:

The rain in Spain falls mainly in the plain.

I'd like to be able to detect, separately and distinctly, clicks on either "Spain" or "plain" and take an appropriate reaction.

When I look at what's available in the Swing API, I'm pretty damn sure that a JLabel itself isn't going to do this for me, but I'm hoping that creative use of something like JEditorPane and/or HTMLDocument or something will do the trick, without me having to reinvent the wheel by parsing the HTML and calculating string pixel widths and all that other crap just to figure out the click regions where each of the links ends up.
Kerry Shetline
Greenhorn

Joined: Feb 09, 2006
Posts: 14
Looks like the UBB code I tried to use didn't work. It sure would have been nice to be able to preview my post before posting it, or at least to be able to edit the post now.

Oh, well. Here's another try at what I was trying to show. Hopefully it will come out looking right and not become another dead, uneditable post:

The hypothetical JLabel should look like this:

The rain in Spain falls mainly in the plain.

The goal is one single GUI component with two distinct clickable actions.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Kerry Shetline:
It's easy enough to turn an entire JLabel into a clickable link for one URL or action. That's not what I'm looking for here. [...]

When I look at what's available in the Swing API, I'm pretty damn sure that a JLabel itself isn't going to do this for me, but I'm hoping that creative use of something like JEditorPane and/or HTMLDocument or something will do the trick, without me having to reinvent the wheel by parsing the HTML and calculating string pixel widths


If you want to do this with a single component, then JEditorPane is the obvious choice because it's the only JComponent that supports HyperlinkListener.



Something else that might work just as well is to use JButtons (without borders) for the clickable things and JLabels for the unclickable things. The layout manager would handle the width stuff for you, but I guess you'd have to do your own parsing.

[edit: break long line for better wrapping]
[ November 28, 2007: Message edited by: Brian Cole ]

bitguru blog
Kerry Shetline
Greenhorn

Joined: Feb 09, 2006
Posts: 14
That works great. Thanks!

One more question: I don't actually want to use real URLs. All I really need are strings which represent action commands. However, if I try something like this:

<a href='spain'> or <a href='#spain'> or <a href='about:spain'>

...then hle.getURL() returns null, presumably because the HREF won't parse. Is there a way for setting a context for parsing URLs, getting the raw HREF even if it doesn't parse, etc... anything so that I can avoid the hack of making fake http URLs like "http://foo.com/action.name"?
Kerry Shetline
Greenhorn

Joined: Feb 09, 2006
Posts: 14
Ah... nevermind. hle.getDescription() does the trick.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making clickable links using HTML in a JLabel
 
Similar Threads
Set color for JLabel
HTML Link in a JLabel?
Setting a JEditorPane to use JLabel's default font when using HTML
What special in 'span' tag?
jdbc-2 loading driver