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.
How can I add a hyperlink to a JFace Dialog
Asked Answered
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.
perfect! yes I needed it for an RCP app so this did the trick nicely :) –
Millham
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. –
Clouet
+1 worked fine in my mac. But in window this link was helpful, as eclipse was not getting java.ui package. –
Weismannism
In case it's missed by anyone ... the link to Google needs the "http://" in front of it. –
Strophe
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 <a href=\"google.com\">Google</a>" Thanks! –
Dyadic
© 2022 - 2024 — McMap. All rights reserved.
PlatformUI.getWorkbench()...
? – Escurial