首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否禁用java JFace对话框中的关闭按钮?

问是否禁用java JFace对话框中的关闭按钮?
EN

Stack Overflow用户
提问于 2010-11-03 02:29:26
回答 4查看 14.9K关注 0票数 3

如何在java JFace对话框中禁用关闭按钮(或使其完全消失)?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-11-03 06:42:12

有关如何在Dialog中隐藏“关闭”按钮的示例,请参见here。您只需覆盖以下方法:

代码语言:javascript
复制
protected void setShellStyle(int arg0){
    //Use the following not to show the default close X button in the title bar
    super.setShellStyle(SWT.TITLE);
}

否则,重写close()并返回false以防止关闭。

更新:虽然上面的代码“解决”了手头的问题,但它没有解释太多,并引入了一个令人讨厌的bug。请参阅谷歌的答案,以获得更好的版本。

票数 8
EN

Stack Overflow用户

发布于 2010-11-03 17:36:39

对话框中的按钮是使用createButton()方法创建的。要“过滤掉”cancel按钮,您可以按如下方式覆盖它:

代码语言:javascript
复制
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);
}

但是,对话框的关闭按钮(由操作系统提供)仍然有效。要禁用它,可以覆盖canHandleShellCloseEvent():

代码语言:javascript
复制
protected boolean canHandleShellCloseEvent() {
    return false;
}

下面是一个完整的最小示例:

代码语言:javascript
复制
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();
    }
}
票数 12
EN

Stack Overflow用户

发布于 2011-10-29 06:15:12

要使X按钮在对话框中不可见,必须关闭SWT.CLOSE style属性。重要的是要注意,这必须在对话框打开之前完成,所以在对话框的构造函数中可以工作。

代码语言:javascript
复制
public NoCloseDialog(...){
    super(...);
    setShellStyle(getShellStyle() & ~SWT.CLOSE);
}

JFace窗口上的默认外壳样式是SWT.SHELL_TRIM,它等同于SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.MAX | SWT.RESIZE

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4080925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档