代码之家  ›  专栏  ›  技术社区  ›  Debajit

在java JFace对话框中禁用关闭按钮?

  •  3
  • Debajit  · 技术社区  · 15 年前

    如何在java JFace对话框中禁用close按钮(或者尽可能使其完全消失)?

    4 回复  |  直到 15 年前
        1
  •  8
  •   the.duckman    14 年前

    here 例如,显示如何在 Dialog

    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以防止关闭。

    更新

        2
  •  12
  •   jastram    15 年前

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

    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():

    protected boolean canHandleShellCloseEvent() {
        return false;
    }
    

    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();
        }
    }
    
        3
  •  9
  •   Greg Leaver    14 年前

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

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

    JFace窗口上的默认shell样式是 SWT.SHELL_TRIM 等于 SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.MAX | SWT.RESIZE

        4
  •  1
  •   Sotiris Adrian J. Moreno    14 年前

    对于从扩展的对话框 org.eclipse.jface.dialogs.Dialog ,超出范围 canHandleShellCloseEvent 然后关闭整个应用程序对于我来说是一个很好的策略,因为如果用户选择取消,我也必须这样做。

    在下面 open() createContents() 方法,

    shell.addListener(SWT.Close, new Listener() { 
    
                public void handleEvent(Event event) {
                    System.exit(0);
                }
    
         });