TAGS :Viewed: 19 - Published at: a few seconds ago

[ How can I add a hyperlink to a JFace Dialog ]

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.

Answer 1


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.