[an error occurred while processing this directive]

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 CreateLinkExample.java

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


[an error occurred while processing this directive]