[閒聊] How to write a hyperlink in Eclipse …
I mean to write a link to allow user click and
open system browser to view the page.
(Like official help page , reference link ....etc)
Like this image
http://goo.gl/GRAz0
You could use Link widget ( org.eclipse.swt.widgets.Link )
The routine is ,
1. create a link widget
2. set text with <a> tag(just like html)
3. add selection listener , (the e.text will be the herf from tag)
4. open browser.
The following sample code below.
===========
Link link = new Link( parent, style );
link.setText("<a href=\"hello" rel="nofollow">http://www.google.com.tw\">hello hyperlink</a>");
link.addSelectionListener(new SelectionListener(){
public void widgetSelected(SelectionEvent e) {
if(e.text!=null && !"".equals(e.text)){
try {
BrowserUtil.openSystemBrowser(null, e.text,false);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
public void widgetDefaultSelected(SelectionEvent e) {
//do nothing
}
});
=============
the browser util class
=============
public class BrowserUtil {
public static IWebBrowser openSystemBrowser(String browserid, String
url,boolean embed) {
try {
IWorkbenchBrowserSupport browserSupport =
PlatformUI.getWorkbench().getBrowserSupport();
IWebBrowser browser = null;
if(embed){
browser = browserSupport.createBrowser(browserid); //$NON-NLS-1$
}else{
browser = browserSupport.getExternalBrowser();
}
browser.openURL(new URL(url));
return browser;
} catch (Exception e) {
return null;
}
}
}
--
網頁上拉近距離的幫手 實現 GMail豐富應用的功臣
數也數不清的友善使用者體驗 這就是javascript
歡迎同好到 AJAX 板一同討論。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 198.203.175.175
※ 編輯: TonyQ 來自: 198.203.175.175 (03/25 03:49)
Eclipse 近期熱門文章
PTT數位生活區 即時熱門文章