Eclipse JFace's Wizards(Again.)

e5nszbig  于 8个月前  发布在  Eclipse
关注(0)|答案(2)|浏览(62)

现在,我am able to set the content of my second wizard's page depending在第一页的选择,我正在寻找一种方法,让给予重点,我的第二页的内容时,用户点击下一步按钮的第一页。
默认情况下,当用户单击Next按钮时,焦点将被赋予按钮组合(Next、Back或Finish按钮,具体取决于向导配置)
我发现的唯一方法是给予重点我的网页的内容是以下之一:

public class FilterWizardDialog extends WizardDialog {

    public FilterWizardDialog(Shell parentShell, IWizard newWizard) {
        super(parentShell, newWizard);
    }

    @Override
    protected void nextPressed() {
        super.nextPressed();
        getContents().setFocus();
    }
}

对我来说,为了实现这种行为而必须重写WizardDialog类有点“无聊和沉重”。WizardDialog javadoc说:
客户端可以子类化WizardDialog,尽管这很少需要。
你觉得这个解决方案怎么样?有没有更简单、更干净的方法来完成这项工作?

q7solyqu

q7solyqu1#

这个thread建议:
在向导页中,使用继承的setVisible()方法,该方法在显示页之前自动调用:

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

postSetFocusOnDialogField方法包含:

/**
 * 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();
                }
            }
        );
    }
}

请注意,Eclipse 4.29(2023年第3季度)

Control.setFocus()不再激活其Shell

之前调用Control.setFocus()也激活了它的Shell。这种行为从未被记录在案,也从未解释过为什么会存在。但这也带来了问题:设置焦点到一个控件在一个背景 shell 导致 shell 跳转到用户。
现在设置焦点只是设置焦点,即使Shell在背景中。要将Shell放在前面,请显式调用Shell.setActive()
可以使用Java属性禁用此更改。
例如,在eclipse.ini中或在-vmargs:之后的命令行上添加此VM参数

-Dorg.eclipse.swt.internal.activateShellOnForceFocus=true

注意:此属性只是临时添加的,稍后将被删除。
参见SWT Issue 450

ogsagwnx

ogsagwnx2#

VonC的答案很好用,我个人觉得这样做更容易:

@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();
            }
        });
    }
}

相关问题