Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWT - Modifying Window Close Button (Red X)

Tags:

java

swt

jface

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);
}
  1. Can you catch the press of the Window Close Button(red X) to use the code above?
  2. Can you just disable the Window close Button(red X)?
like image 757
jkteater Avatar asked Nov 02 '25 21:11

jkteater


2 Answers

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.

like image 132
Baz Avatar answered Nov 05 '25 09:11

Baz


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).

like image 27
sambi reddy Avatar answered Nov 05 '25 09:11

sambi reddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!