Java – SWT – modify window close button (red x)

When the user closes any application window using the window close button (red x) It will cause the widget is disposed problem in my application When they close the window using the close application I provided business as usual.

@Override
protected void createButtonsForButtonBar(Composite parent) {
   createButton(parent,IDialogConstants.OK_ID,"Close Aplot",true);
}

@Override
protected void okPressed() {
   getShell().setVisible(false);
}

>Can you grasp the window close button (red x) and press it to use the above code? > Can you disable the window close button (red x)?

Solution

Listen to SWT on shell Close.

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;
            Message@R_71_2419@ message@R_71_2419@ = new Message@R_71_2419@(shell,style);
            message@R_71_2419@.setText("Information");
            message@R_71_2419@.setMessage("Close the shell?");
            event.doit = message@R_71_2419@.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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>