views:

156

answers:

2

I'm working on a Eclipse RCP Application. 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;
     } 
    }
   }
  }
A: 

Have you tried Control#forceFocus()?

Kire Haglin
+1  A: 

The setFocus() may return false on the following situations:

  1. Maybe control is a unfocusable control like Label
  2. Composites attempt to assign focus to their children before taking focus themselves
  3. A control will not take focus if it is disabled or hidden
  4. 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.

Suraj Chandran
Perhaps you are calling the `setFocus()` before the `Shell` is visible?
Paul Lammertsma