在 Java JFace 对话框中禁用关闭按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4080925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable close button in java JFace dialog?
提问by PlagueHammer
How can i disable the close button (or make it disappear completely if possible) in a java JFace dialog?
如何在 Java JFace 对话框中禁用关闭按钮(或尽可能让它完全消失)?
回答by jastram
Buttons in a Dialog are created with the method createButton(). To "filter out" the cancel button, you can override it as follows:
对话框中的按钮是使用 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);
}
However, the Dialog's close button (provided by the OS) still works. To disable it, you can override canHandleShellCloseEvent():
但是,对话框的关闭按钮(由操作系统提供)仍然有效。要禁用它,您可以覆盖 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();
}
}
回答by Greg Leaver
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.
要使 X 按钮在对话框上不可见,您必须关闭 SWT.CLOSE 样式属性。重要的是要注意,这必须在打开对话框之前完成,因此在对话框的构造函数中将起作用。
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
JFace Window 上的默认外壳样式SWT.SHELL_TRIM
等于SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.MAX | SWT.RESIZE
回答by the.duckman
See herefor an example showing how to hide the close button in a Dialog
. You simply override following method:
请参阅此处的示例,了解如何在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);
}
Otherwise override close()
and return false to prevent closing.
否则覆盖close()
并返回 false 以防止关闭。
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.
更新:虽然上面的代码“解决”了手头的问题,但它没有解释太多,并引入了一个讨厌的错误。请参阅 Goog 的回答,以获得更好的版本。
回答by Sudhish Ullattil
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.
对于从 扩展的对话框org.eclipse.jface.dialogs.Dialog
,overrdingcanHandleShellCloseEvent
对我不起作用,然后关闭整个应用程序对我的情况来说是一个很好的策略,因为如果用户选择取消,我必须这样做。
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,
根据open()
或createContents()
方法,
shell.addListener(SWT.Close, new Listener() {
public void handleEvent(Event event) {
System.exit(0);
}
});