When a user closes any of the application windows using the Window Close Button(red X) button. It causes Widget is Disposed issues with my application. When they close the window using the close application I provided. Everything works correctly.
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, "Close Aplot", true);
}
@Override
protected void okPressed() {
getShell().setVisible(false);
}
Listen for SWT.Close
on the Shell
.
This should help:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
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.setText("Information");
messageBox.setMessage("Close the shell?");
event.doit = messageBox.open() == SWT.YES;
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
It will prompt the user to verify the decision.
In a jface dialog, always it will invoke close()
method irrespective of where OK is pressed or CANCEL is pressed or close(red x button) is clicked.
Override close method and check return code ( use getReturnCode
() method).
First thing I do is override the close() method in the ApplicationWindow
/** the rc from the message dialog */
int rc = -1;
@Override
public boolean close()
{
MessageDialog messagedialog = new MessageDialog(getShell(),
"Confirm Exit", null, "Are you sure you want to exit?", 4,
new String[]
{ "Yes", "No" }, 1);
messagedialog.setBlockOnOpen(true);
messagedialog.open();
rc = messagedialog.getReturnCode();
/** int to hold the return code from the message dialog */
if (rc == 0)
{
return true;
} else
{
return false;
}
}
The second thing I do is listen for events on the 'X' close button
shell.addListener(SWT.Close, new Listener()
{
@Override
public void handleEvent(Event event)
{
switch (rc)
{
case 0: // yes pressed
event.doit = true;
break;
case 1: // no pressed
event.doit = false;
break;
case -1: // escape pressed
break; // do nothing
default:
break;
}
}
});
The "event.doit" boolean field determines the closure of the shell. If the value is equal to 'true' - "lets doit", lets close the shell (ie rc = 0).
If this value is equal to 1 (rc = 1) the shell is left open.
If ESC is pressed (ie the rc = -1) we simply do nothing. But we could minimize the shell if this was a requirement or perform some other action based on this event.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.