Pla*_*mer 3 java user-interface swt jface
如何在java JFace对话框中禁用关闭按钮(如果可能,使其完全消失)?
jas*_*ram 12
使用createButton()方法创建Dialog中的按钮.要"过滤"取消按钮,您可以按如下方式覆盖它:
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);
}
Run Code Online (Sandbox Code Playgroud)
但是,Dialog的关闭按钮(由操作系统提供)仍然有效.要禁用它,您可以覆盖canHandleShellCloseEvent():
protected boolean canHandleShellCloseEvent() {
return false;
}
Run Code Online (Sandbox Code Playgroud)
这是一个完整的,最小的例子:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
要使对话框上的X按钮不可见,您必须关闭SWT.CLOSE样式属性.需要注意的是,必须在打开对话框之前完成此操作,因此在对话框的构造函数中可以正常工作.
public NoCloseDialog(...){
super(...);
setShellStyle(getShellStyle() & ~SWT.CLOSE);
}
Run Code Online (Sandbox Code Playgroud)
JFace窗口上的默认shell样式SWT.SHELL_TRIM等于SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.MAX | SWT.RESIZE
请参阅此处以获取显示如何隐藏关闭按钮的示例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);
}
Run Code Online (Sandbox Code Playgroud)
否则覆盖close()并返回false以防止关闭.
更新:虽然上面的代码"解决"了手头的问题,但它没有解释很多,并引入了一个令人讨厌的错误.请参阅Goog的答案,以获得更好的版本.
| 归档时间: |
|
| 查看次数: |
13978 次 |
| 最近记录: |