flavio.donze
flavio.donze

Reputation: 8100

Where to set the focus in wizard pages?

I have a Wizard containing two wizard pages (org.eclipse.jface.wizard.WizardPage) and would like to set the focus for each page separately so that always the top input field of each page is focused.

Setting the focus in WizardPage.createControl(Composite), the first page focus is set correctly. The second page does not have a focus. This is due to Wizard.createPageControls(Composite) which creates all the pages at the beginning.

Where would be the place to handle the focus after switched to the next wizard page?

Upvotes: 1

Views: 517

Answers (2)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 21025

JFace wizards don't offer a designated hook to set the focus. However, as Greg already mentioned, the setVisible() method can be used to set the initial focus of a wizard page.

Usually, the focus of wizard pages should only be set when showing the page for the first time. If a user returns back to a page, the focus should remain where it was when the page was left.

Therefore, I usually guard the focus code so that it is only executed when the page is shown for the first time:

private boolean firstTimeShown = true;

@Override
public void setVisible( boolean visible ) {
  super.setVisible( visible );
  if( visible && firstTimeShown ) {
    firstTimeShown = False;
    control.setFocus();
  }
}

Upvotes: 1

greg-449
greg-449

Reputation: 111216

Override the WizardPage setVisible method and set focus when the page becomes visible:

@Override
public void setVisible(boolean visible) {
    super.setVisible(visible);

    if (visible) {
       // TODO set focus
    }
}

Upvotes: 3

Related Questions