SelectionListener and DisposeListener : Event « SWT JFace Eclipse « Java

SelectionListener and DisposeListener

SelectionListener and DisposeListener

//Send questions, comments, bug reports, etc. to the authors:

//Rob Warner (
//Robert Harris (

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

 * This class demonstrates SelectionListener and DisposeListener
public class DisposeListenerExample {
   * The application entry point
   * @param args the command line arguments
  public static void main(String[] args) {
    Display display = new Display();

    // Create the main window
    Shell mainShell = new Shell(display);
    mainShell.setLayout(new FillLayout());
    mainShell.setText("Big Brother");
    final Label mainMessage = new Label(mainShell, SWT.LEFT);
    mainMessage.setText("Don't even think about it");

    // Create the child shell and the dispose listener
    final Shell childShell = new Shell(mainShell);
    childShell.addDisposeListener(new DisposeListener() {
      public void widgetDisposed(DisposeEvent event) {
        // When the child shell is disposed, change the message on the main shell
    childShell.setLayout(new FillLayout());
    childShell.setText("little brother");

    // Put a message on the child shell
    new Label(childShell, SWT.LEFT)
        .setText("If you dispose me, my big brother's gonna get you!");

    // Add a button and a listener to the child shell
    Button button = new Button(childShell, SWT.PUSH);
    button.setText("Close Me!");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        // When the button is pressed, close the child shell

    // Open the shells;;

    while (!mainShell.isDisposed()) {
      if (!display.readAndDispatch()) {


Related examples in the same category

1.ModifyEvent: Temperature Converter JFaceModifyEvent: Temperature Converter JFace
2.Mouse Event Listener Mouse Event Listener
3.Utility class for event handling
4.Demonstrates various listenersDemonstrates various listeners
5.Demonstrates mouse eventsDemonstrates mouse events
6.Demonstrates ControlListenersDemonstrates ControlListeners
7.Demonstrates FocusListenerDemonstrates FocusListener
8.Demonstrates LineBackgroundListenersDemonstrates LineBackgroundListeners
9.Key Listener Example