Selection Events : Button Event « SWT « Java Tutorial






Selection events are generated when buttons get selected and pressed. There are two methods defined in the SelectionListener interface:

public void widgetSelection(SelectionEvent e)
    public void widgetDefaultSelection(SelectionEvent e)
  1. The first method is called when a button is selected.
  2. Because default selection is not applicable to buttons.
Selection Events
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class EventSelection {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new FillLayout());

    Button button = new Button(shell, SWT.RIGHT);

    button.setText("text on the button");

    button.addSelectionListener(new SelectionListener() {

      public void widgetSelected(SelectionEvent arg0) {
        System.out.println("selected");
      }

      public void widgetDefaultSelected(SelectionEvent arg0) {
      }
    });

    shell.open();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }

}








17.10.Button Event
17.10.1.Selection EventsSelection Events
17.10.2.To add a listener to a buttonTo add a listener to a button