如何在java JFace对话框中禁用关闭按钮(或使其完全消失)?
发布于 2010-11-03 06:42:12
有关如何在Dialog中隐藏“关闭”按钮的示例,请参见here。您只需覆盖以下方法:
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。请参阅谷歌的答案,以获得更好的版本。
发布于 2010-11-03 17:36:39
对话框中的按钮是使用createButton()方法创建的。要“过滤掉”cancel按钮,您可以按如下方式覆盖它:
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();
}
}发布于 2011-10-29 06:15:12
要使X按钮在对话框中不可见,必须关闭SWT.CLOSE style属性。重要的是要注意,这必须在对话框打开之前完成,所以在对话框的构造函数中可以工作。
public NoCloseDialog(...){
super(...);
setShellStyle(getShellStyle() & ~SWT.CLOSE);
}JFace窗口上的默认外壳样式是SWT.SHELL_TRIM,它等同于SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.MAX | SWT.RESIZE
https://stackoverflow.com/questions/4080925
复制相似问题