mydomain
No ADS
No ADS

Java SWT Link Tutorial with Examples

  1. SWT Link
  2. SWT Link Example

1. SWT Link

SWT Link represents the hyper links similar to the anchor links on the HTML.
// Create a Link
Link link = new Link(shell, SWT.NONE);
link.setText("Go to <a href=\"htpps://eclipse.org\">Eclipse</a> Home Page.");

// Event handling when users click on links.
link.addSelectionListener(new SelectionAdapter()  {
    @Override
    public void widgetSelected(SelectionEvent e) {
        Program.launch("https:/eclipse.org");
    }
});

2. SWT Link Example

No ADS
The following example creates a hyperlink that when you click on it, it go to the home page of Eclipse (https://eclipse.org).
LinkDemo.java
package org.o7planning.swt.link;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;

public class LinkDemo {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("SWT Link (o7planning.org)");
        
        RowLayout rowLayout= new RowLayout();
        rowLayout.marginLeft=10;
        rowLayout.marginTop=10;
        shell.setLayout(new RowLayout());
        // Create a Link
        Link link = new Link(shell, SWT.NONE);
        link.setText("Go to <a href=\"htpps://eclipse.org\">Eclipse</a> Home Page.");
        
        // Event handling when users click on links.
        link.addSelectionListener(new SelectionAdapter()  {
            @Override
            public void widgetSelected(SelectionEvent e) {
                Program.launch("https:/eclipse.org");
            }
        });
        shell.setSize(400, 250);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
No ADS
No ADS