i am using swt with java and am having a problem with a message box. when i run my program a login shell appears. when a user clicks the close button (X) in top right hand corner of the login shell they are asked if they really want to exit. when they click yes the application exits which is what i want to happen, but when they click no i just want the login shell to remain, however it keeps closing and open the shell that should open upon successful login.

can anyone please help me.

thanks in advanced,


Sent the code via private message. i basically just want the messge box to close and the login screen to remain.

thanks for replying to me.

I don't have the swt (standard widget toolkit) which normally comes with the eclipse ide. So I cannot help here. I've posted the code so others may.

It seems like you may be missing a few braces as well.

public class Login
    Display display = new Display ();
    final Shell shell = new Shell (display);

    final Label welcomeLabel, label1, label2;
    final Button button1;
    final Text textField1, textField2;

    public Login()

        label1 = new Label(shell, SWT.BOLD);
        label1.setText("Investigator ID: ");
        label1.setSize(100,20); // (width, height)
        label1.setLocation(20,60); // (x,y) co-ordinates

        textField1 = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP);
        textField1.setSize(90,20); // (width, height)
        textField1.setLocation(120,60); // (x,y) co-ordinates

        textField1.addListener(SWT.KeyDown, new Listener()
            public void handleEvent(Event event){}

        textField1.addListener(SWT.Traverse, new Listener()
            public void handleEvent(Event event)
                event.doit = true;

        button1 = new Button(shell, SWT.BUTTON1);
        button1.setSize(90,30); // (width, height)
        button1.setLocation(220,75); // (x,y) co-ordinates

        shell.setSize(340, 340); // (width, height)
        shell.open ();

        shell.addListener(SWT.Close, new Listener()
            public void handleEvent(Event event)

            int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO;
            MessageBox messageBox = new MessageBox(shell, style);
            messageBox.setMessage("Do You Wish to Close the Application?");
//event.doit = messageBox.open() == SWT.YES;

            int response = messageBox.open();

            switch (response)
                case SWT.YES:
                case SWT.NO:
                // tried to just go back to login screen

            } // end switch
        } // end handle event
    }); // end addListener

        while (!shell.isDisposed ())
            if (!display.readAndDispatch ()) display.sleep();
        display.dispose ();
        new Shell(); // call another class that opens another shell

    } // end constructor

SelectionListener login = new SelectionListener()
    public void widgetSelected(SelectionEvent arg0)
    String response1 = textField1.getText();
    String response2 = textField2.getText();
    if (!response1.equals("") && !response2.equals(""))
        new Database(response1, response2);

        MessageBox messageBox = new MessageBox(shell, SWT.ICON_WARNING);
        messageBox.setMessage("You Must Enter Both Investigator ID & Password");

            int buttonID = messageBox.open();

public void widgetDefaultSelected(SelectionEvent arg0) 
}; // end SelectionListener() method
This article has been dead for over six months. Start a new discussion instead.