简体   繁体   中英

How do I change the focus on buttons of JFace WizardPage?

I have implemented a JFace Wizard with 2 WizardPages.

By default, the first WizardPage has these 4 Buttons:

  • Back (disabled)
  • Next (focused)
  • Cancel
  • Finish (disabled)

Now I want to set the default focus on the Cancel Button. How do I do that?

Removing the focus and setting it to some Control of the page's content would also be ok.

I tried setting the focus to a Button in the content layout of the WizardPage, but this only sets me a second focus on the immediateButton . The focus on the Next button is still there, and the Next button reacts to pressing enter, which is what I want to avoid.

@Override
public void setVisible(boolean visible) {
    super.setVisible(visible);
    if (visible) {
        immediateButton.setFocus();
    }
}

How can I access the Dialog buttons and change their focus?

The Next button does not actually have focus, rather it is the Shell Default button.

The logic in WizardDialog makes either Next or Finish the default button and there does not seem to be a way to change this.

You may be able to override this by calling getShell().setDefaultButton(button) in your wizard page.

Update: Testing this you can do it in setVisible but you need to use Display.asyncExec to make the code run at the right time:

final Shell shell = getShell();

shell.getDisplay().asyncExec(() -> shell.setDefaultButton(immediateButton));

above is for Java 8, for Java 7 or earlier:

shell.getDisplay().asyncExec(new Runnable()
   {
     @Override
     public void run()
     {
       shell.setDefaultButton(immediateButton); 
     }
   });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM