How can I make a hyperlink in a JFace Dialog that when clicked opens the link in the default web browser. A full example would be useful. I know there is a org.eclipse.jface.text.hyperlink
package but I can't find a suitable example.
Asked
Active
Viewed 1.1k times
12

Rüdiger Herrmann
- 20,512
- 11
- 62
- 79

Alb
- 3,601
- 6
- 33
- 38
1 Answers
20
Are you running an RCP application?
If so, then the following code will open your link in the default OS browser:
// 'parent' is assumed to be an SWT composite
Link link = new Link(parent, SWT.NONE);
String message = "This is a link to <a href=\"www.google.com\">Google</a>";
link.setText(message);
link.setSize(400, 100);
link.addSelectionListener(new SelectionAdapter(){
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("You have selected: "+e.text);
try {
// Open default external browser
PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(new URL(e.text));
}
catch (PartInitException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
catch (MalformedURLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
});
The above assumes that you do not want to scan existing text for hyperlinks but simply wish to create one programmatically. If the former is required then you'll need to use the API from JFace text packages or suchlike.

tbone
- 581
- 3
- 4
-
What is the part which requires RCP? The `PlatformUI.getWorkbench()...`? – Mot Oct 24 '10 at 13:40
-
perfect! yes I needed it for an RCP app so this did the trick nicely :) – Alb Oct 24 '10 at 14:29
-
mklhmm: Yes, the PlatformUI.getWorkbench() call requires the org.eclipse.ui package which is part of the RCP SDK. I'm glad that this worked for you Alb. – tbone Oct 25 '10 at 09:27
-
+1 worked fine in my mac. But in window this [link](http://stackoverflow.com/questions/10967451/open-a-link-in-browser-with-java-button) was helpful, as eclipse was not getting java.ui package. – HDdeveloper Feb 12 '13 at 07:26
-
1In case it's missed by anyone ... the link to Google needs the "http://" in front of it. – David G Jul 09 '13 at 16:43
-
Thanks tbone, your answer helped me a lot. I would suggest a little change in the "message" string since the one you use throws a MalformedURLException. String message = "This is a link to Google" Thanks! – user2033666 Dec 18 '13 at 18:42