setFocus() 始终为 false
-
23-09-2019 - |
题
我正在开发 Eclipse RCP 应用程序。在扩展 MultiPageEditorPart 的类中,我尝试将焦点设置到文本字段。但 setFocus 方法总是返回 false。
我究竟做错了什么?
MultiPageEditor 有各种页面,在这些页面内有 Composite 类。这些类包含文本字段。
这是片段:(errorPage 是一个 int,我的验证发现错误的页码)
if(!dataValid) {
MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Fehler bei der Dateneingabe", stringBuilder.toString());
this.setActivePage(errorPage);
Composite errorComposite = (Composite) this.getControl(errorPage);
Control[] children = errorComposite.getChildren();
for (Control child : children) {
if(child instanceof Form) {
Form form = (Form) child;
Composite body = form.getBody();
Control[] formChildren = body.getChildren();
for (Control formChild : formChildren) {
if(formChild.equals(errorControl))
formChild.setFocus();
return dataValid;
}
}
}
}
解决方案
setFocus() 在以下情况下可能返回 false:
- 也许控件是像 Label 这样的不可聚焦控件
- 复合体尝试在自己获得焦点之前将焦点分配给子级
- 如果控件被禁用或隐藏,它将不会获得焦点
- 由于模态,输入被阻止。
所以我最好检查一下,(1)我是否将焦点设置在右侧控件上,(2)控件是否可见,也许包含该控件的表单不在当前选定的选项卡中。(3) 是否打开任何其他模式对话框。
其他提示
您是否尝试过控制#forceFocus()?
不隶属于 StackOverflow