setFocus() always false
I'm working on a Eclipse RCP Appli开发者_如何学Pythoncation. In a class which extends MultiPageEditorPart, I'm trying to set the focus to a text field. But the setFocus Method always returns false.
What am I doing wrong?
The MultiPageEditor has various pages and inside these pages, there are Composite - classes. These classes contain the text field.
Here is the snippet: (errorPage is an int, the Pagenumber on which my validation found the error)
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;
}
}
}
}
The setFocus() may return false on the following situations:
- Maybe control is a unfocusable control like Label
- Composites attempt to assign focus to their children before taking focus themselves
- A control will not take focus if it is disabled or hidden
- Input is blocked due to modality.
So I would better check, (1) am I setting focus on the right control, (2) is the control visible, maybe the form containing the control is not in the current selected tab. (3) is any other modal dialog open.
Have you tried Control#forceFocus()?
精彩评论