HyperlinkLabel - Haixing-Hu/swt-widgets GitHub Wiki
A HyperlinkLabel
can be use as a label looks like and works as a hyperlink.
This is the Hyperlink
widget comes from the Novocode SWT Controls, except that this implementation removes the support of mnemonics (since I think it is useless).
Usage
Here is the code snippet:
final Label label1 = new Label(shell, SWT.NONE);
label1.setText("Contact me at ");
label1.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
final HyperlinkLabel link1 = new HyperlinkLabel(shell,
SWT.SINGLE | SWT.BORDER | SWT.NO_FOCUS);
link1.setText("[email protected]");
link1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
link1.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
DesktopApi.mail(link1.getText());
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
DesktopApi.mail(link1.getText());
}
});
final Label label2 = new Label(shell, SWT.NONE);
label2.setText("Google: ");
label2.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
final HyperlinkLabel link2 = new HyperlinkLabel(shell,
SWT.SINGLE | SWT.BORDER | SWT.NO_FOCUS);
link2.setText("https://www.google.com");
link2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
link2.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
DesktopApi.open(link2.getText());
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
DesktopApi.open(link2.getText());
}
});
Note that in the above code snippet, the DesktopApi class is provided by the java-commons project.
Example
An example is located in the source repository:
src/test/java/com/github/haixing_hu/swt/label/HyperlinkLabelExample.java