views:

31

answers:

1

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.

+2  A: 

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
What is the part which requires RCP? The `PlatformUI.getWorkbench()...`?
mklhmnn
perfect! yes I needed it for an RCP app so this did the trick nicely :)
Alb
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