Eclipse JFace Wizards (again ...)

Now that I can set the contents of my second page of the wizard depending on the choice of the first page, I’m looking for a way to focus on my Content of the second page when the user clicks the next button on the first page.

By default, when the user presses the next button, the focus is assigned to the composite button (next, back or end) depending on the configuration of the wizard.

The only way I found to focus on my page content is this:

public class FilterWizardDialog extends WizardDialog { public FilterWizardDialog(Shell parentShell, IWizard newWizard) { super(parentShell, newWizard); } @Override protected void nextPressed() { super.nextPressed(); getContents().setFocus(); } } 

It’s a little “boring and hard” for me to override the WizardDialog class to implement this behavior. Moreover, javadoc WizardDialog says:

Clients can be a subclass of WizardDialog , although this is rarely required.

What do you think of this decision? Is there an easier and cleaner solution to do this job?

+4
source share
2 answers

This thread offers:

On the wizard page, use the inherited setVisible() method, which is called automatically before your page displays:

 public void setVisible(boolean visible) { super.setVisible(visible); // Set the initial field focus if (visible) { field.postSetFocusOnDialogField(getShell().getDisplay()); } } 

The postSetFocusOnDialogField method contains:

 /** * Posts <code>setFocus</code> to the display event queue. */ public void postSetFocusOnDialogField(Display display) { if (display != null) { display.asyncExec( new Runnable() { public void run() { setFocus(); } } ); } } 
+7
source

VonC's answer works great, I personally find it easier to work with:

 @Override public void setVisible(boolean visible) { super.setVisible(visible); if (visible) { Control control = getControl(); if (!control.setFocus()) { postSetFocus(control); } } } private void postSetFocus(final Control control) { Display display = control.getDisplay(); if (display != null) { display.asyncExec(new Runnable() { @Override public void run() { control.setFocus(); } }); } } 
0
source

Source: https://habr.com/ru/post/1285945/


All Articles