Johnas
Johnas

Reputation: 336

How to add hyperlink to a Java SWT Label

I have a org.eclipse.swt.widgets.Label and I want to add a Hyperlink to a specific portion of the Label Text. For example to have something like: "Click HERE" and when I click on HERE then a specific window will open.

If it doesn't work with the Label, then what else can I use to have a Hyperlink? Thank you!

Upvotes: 1

Views: 1347

Answers (1)

greg-449
greg-449

Reputation: 111217

Label is just plain text.

The Link class (org.eclipse.swt.widgets.Link) allows text and links. The Javadoc for Link.setText says:

The string can contain both regular text and hyperlinks. A hyperlink is delimited by an anchor tag, <a> and </a>. Within an anchor, a single HREF attribute is supported. When a hyperlink is selected, the text field of the selection event contains either the text of the hyperlink or the value of its HREF, if one was specified. In the rare case of identical hyperlinks within the same string, the HREF attribute can be used to distinguish between them. The string may include the mnemonic character and line delimiters. The only delimiter the HREF attribute supports is the quotation mark ("). Text containing angle-bracket characters < or > may be escaped using \, however this operation is a hint and varies from platform to platform.

You must listen to the selection event from this control and add code to open the link.

In an Eclipse plug-in you can use the Eclipse Forms FormText and Hyperlink controls which also allow mixed text and links plus other formatting.

Upvotes: 2

Related Questions