문제

How can i disable the close button (or make it disappear completely if possible) in a java JFace dialog?

도움이 되었습니까?

해결책

See here for an example showing how to hide the close button in a Dialog. You simply override following method:

protected void setShellStyle(int arg0){
    //Use the following not to show the default close X button in the title bar
    super.setShellStyle(SWT.TITLE);
}

Otherwise override close() and return false to prevent closing.

Update: While the above code "solves" the problem at hand, it doesn't explain a lot, and introduces a nasty bug. Please see Goog's answer, for a way better version.

다른 팁

Buttons in a Dialog are created with the method createButton(). To "filter out" the cancel button, you can override it as follows:

protected Button createButton(Composite parent, int id,
        String label, boolean defaultButton) {
    if (id == IDialogConstants.CANCEL_ID) return null;
    return super.createButton(parent, id, label, defaultButton);
}

However, the Dialog's close button (provided by the OS) still works. To disable it, you can override canHandleShellCloseEvent():

protected boolean canHandleShellCloseEvent() {
    return false;
}

Here is a complete, minimal example:

package stackoverflow;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class JFaceDialogNoCloseButton {
    private static final Display DISPLAY = Display.getDefault();

    public static void main(String[] args) {
        Shell shell = new Shell(DISPLAY, SWT.CLOSE | SWT.RESIZE);
        shell.setSize(200, 100);
        shell.setLayout(new FillLayout());

        final Dialog dialog = new InputDialog(shell, "Title", "Message",
                "initial value", null) {
            @Override
            protected Button createButton(Composite parent, int id,
                    String label, boolean defaultButton) {
                if (id == IDialogConstants.CANCEL_ID)
                    return null;
                return super.createButton(parent, id, label, defaultButton);
            }

            @Override
            protected boolean canHandleShellCloseEvent() {
                return false;
            }
        };

        Button button = new Button(shell, SWT.PUSH);
        button.setText("Launch JFace Dialog");
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                dialog.open();
            }
        });

        shell.open();

        while (!shell.isDisposed()) {
            if (!DISPLAY.readAndDispatch()) {
                DISPLAY.sleep();
            }
        }
        DISPLAY.dispose();
    }
}

To make the X button not visible on a Dialog you have to turn off the SWT.CLOSE style property. Important to note that this has to be done before the dialog is opened, so in the constructor of your dialog would work.

public NoCloseDialog(...){
    super(...);
    setShellStyle(getShellStyle() & ~SWT.CLOSE);
}

The default shell style on a JFace Window is SWT.SHELL_TRIM which is equal to SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.MAX | SWT.RESIZE

For a dialog extended from org.eclipse.jface.dialogs.Dialog, overrding canHandleShellCloseEvent did not work for me, Then closing the entire application worked as a good strategy for my situation, because I had to do the same if the user choose to cancel.

I know this is not the exact answer to the question but can be used as a workaround to handle the situation.

under open() or createContents() method,

shell.addListener(SWT.Close, new Listener() { 

            public void handleEvent(Event event) {
                System.exit(0);
            }

     });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top