Clickable link in an application/applet

It seems like it would be a common question, but I have not been able
to find a good reference.

I would like to have a clickable URL link in a JLabel. It's OK if the
whole label is a link.

Ideally, the solution is implemented identically if the label is used
in an applet or an application. I would like to be able to specify the
target. The linked page should be displayed by the user's default
browser.

Advertisements

Aaron Fude wrote:
> Hi,
>
> It seems like it would be a common question, but I have not been able
> to find a good reference.
>
> I would like to have a clickable URL link in a JLabel. It's OK if the
> whole label is a link.

Advertisements

Aaron Fude wrote:
> Hi,
>
> It seems like it would be a common question, but I have not been able
> to find a good reference.
>
> I would like to have a clickable URL link in a JLabel. It's OK if the
> whole label is a link.
>
> Ideally, the solution is implemented identically if the label is used
> in an applet or an application. I would like to be able to specify the
> target. The linked page should be displayed by the user's default
> browser.
>
> Many thanks in advance!
>
> Aaron Fude

This will work in an Applet but I'm sure not in an application. I
remember asking this question years ago but I don't remember the answer
I got. It may have been better than this one .

Aaron Fude wrote:
...
>It seems like it would be a common question, but I have not been able
>to find a good reference.

It is a common question with no simple, straight forward answer.
>I would like to have a clickable URL link in a JLabel.

What sort of information are you opening in the link?
Is it from the your site? Do you have control over it?
>..It's OK if the whole label is a link.

This is easy enough with regular label with a MouseListener
attached, the mouseEntered event can be used to show the link
as 'active'.
>Ideally, the solution is implemented identically if the label is used
>in an applet or an application. I would like to be able to specify the
>target.

That makes it tricky. None of the methods of opening a browser
from an *application* include the option to specify 'target'.
>..The linked page should be displayed by the user's default
>browser.

OK, you ready?

Applet can use the showDocument method, but browsers never
had to implement it, there is no report of success or failure, and
many pop-up killers interfere with it. It does allow to specify a
target, which can be ignored by the browser if it chooses.

I would avoid it, if the information in the links is anything more
than trivial.

Applications.

Pre 1.6 applications would have to rely on BrowserLauncher2
(3rd party APIs), while 1.6+ can use the Desktop.browse()
functionality. Desktop reports success/failure, AFAIR, BL2
throws exceptions if it cannot find(/launch) the browser.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!