我正在开发 Eclipse RCP 应用程序。在扩展 MultiPageEditorPart 的类中,我试图将焦点设置到文本字段。但是 setFocus 方法总是返回 false。
我做错了什么?
MultiPageEditor 有各种页面,在这些页面中,有 Composite - 类。这些类包含文本字段。
这是片段:(errorPage 是一个整数,我的验证发现错误的页码)
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这样的无焦点控件
- 复合型尝试在自己集中注意力之前将注意力分配给他们的 child
- 控件如果被禁用或隐藏则不会获得焦点
- 由于模态问题,输入被阻止。
所以我最好检查一下,(1) 我是否将焦点设置在正确的控件上,(2) 控件是否可见,也许包含该控件的表单不在当前选定的选项卡中。 (3) 是否打开了任何其他模态对话框。
关于java - setFocus() 始终为假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2148143/