Share on Google+Share on Google+

How to create Link in SWT

In this section, you will study how to create Link in a text.

How to create Link in SWT


In this section, you will study how to create Link in a text.

SWT allows to create link by using the class Link of package org.eclipse.swt.widgets. The class Link displays the specified text with links. The method setText(message) sets the text specified. 

Following code calls the Event class and print the selected link on the console:

ink.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("You have selected: "+event.text);

Here is the code of

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;

  public class CreateLinkExample{

  public static void main(String[] args) {
  Display display = new Display();
  Shell shell = new Shell(display);
  Link link = new Link(shell, SWT.NONE);
  String message = "Java is an <a>Platform Independent</a>, 
  <a>Robust</a>,<a>Object Oriented</a>Programming Language.
   Also have<a> Automatic Memory Management.</a>"
  link.addListener(SWT.Selection, new Listener() {
  public void handleEvent(Event event) {
  System.out.println("You have selected: "+event.text);
  shell.setText("Show Link");;
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch())

Output will be displayed as:

On selecting Platform Independent, it will be printed on the console.

Download Source Code



Posted on: October 3, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.