如何在JFace对话框中添加超链接?

时间:2022-06-01 19:53:46

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.

如何在JFace对话框中创建超链接,当单击该对话框时,将在默认web浏览器中打开该链接。一个完整的例子是有用的。我知道有一个org。eclipsee。jface。text。超链接包,但是我找不到一个合适的例子。

1 个解决方案

#1


20  

Are you running an RCP application?

您正在运行RCP应用程序吗?

If so, then the following code will open your link in the default OS browser:

如果是,那么下面的代码将在默认的OS浏览器中打开您的链接:

 // '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.

上面假设您不希望扫描现有的超链接文本,而只是希望以编程方式创建一个超链接。如果需要前者,则需要使用JFace文本包或类似的API。

#1


20  

Are you running an RCP application?

您正在运行RCP应用程序吗?

If so, then the following code will open your link in the default OS browser:

如果是,那么下面的代码将在默认的OS浏览器中打开您的链接:

 // '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.

上面假设您不希望扫描现有的超链接文本,而只是希望以编程方式创建一个超链接。如果需要前者,则需要使用JFace文本包或类似的API。