Change the default behavior of shells : Shell Event « SWT « Java Tutorial

Change the default behavior of shells
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

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

    shell.addShellListener(new ShellListener() {

      public void shellActivated(ShellEvent event) {

      public void shellClosed(ShellEvent event) {
        MessageBox messageBox = new MessageBox(shell, SWT.APPLICATION_MODAL | SWT.YES | SWT.NO);
        messageBox.setMessage("You have unsaved data. Close the shell anyway?");
        if ( == SWT.YES)
          event.doit = true;
          event.doit = false;

      public void shellDeactivated(ShellEvent arg0) {

      public void shellDeiconified(ShellEvent arg0) {

      public void shellIconified(ShellEvent arg0) {
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue


17.5.Shell Event
17.5.1.Shell Events
17.5.2.Change the default behavior of shellsChange the default behavior of shells
17.5.3.Add Close event for Shell window
17.5.4.Add Resize listener to ShellAdd Resize listener to Shell