devstory

Le Tutoriel de Java SWT Link

  1. SWT Link
  2. Exemple avec SWT Link

1. SWT Link

SWT Link représente les hyperliens similaires aux liens d'ancrage sur le 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. Exemple avec SWT Link

L'exemple suivant crée un Hyperlink qui, lorsque vous cliquez dessus, va à la page d'accueil d'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();
    }
}