【问题标题】:Why is the eclipse swt browser not focused?为什么eclipse swt浏览器不集中?
【发布时间】:2020-09-16 05:22:40
【问题描述】:

我正在开发一个 Eclipse RCP 应用程序。我有一个从 IWorkbenchPreferencePage 继承的首选项页面。在首选项页面中,我有一个按钮,单击该按钮会生成一个外壳。 shell 始终处于失焦状态,并且在设置首选项页面之前无法与之交互。

有什么方法可以将焦点设置在shell上?

这里是一些偏好页面的伪代码:

public class PreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

  AuthenticationManager authenticationManager = new AuthenticationManager();

  @Override
  protected void createFieldEditors() {
    final Button login = new Button(getFieldEditorParent(), SWT.PUSH);
    login.setText("Login")
    login.addSelectionListener(new SelectionListener() {
      @Override
      public void widgetSelected(final SelectionEvent se) {
        authenticationManager.run(getShell().getDisplay());
      }
    }
  }
}

public AuthenticationManager {
  public void run(@Nullable Display optionalDisplay) {
    display.asyncExec(() -> {
      // set the url & add listeners
    }
  }
}

谢谢!

【问题讨论】:

  • 你是如何创建外壳的? shell 的父级是什么?首选项对话框是应用程序模式,只有以对话框为父级的 shell 才能获得焦点。
  • 如果你想继续这个问题,你需要回答我之前评论中的问题。

标签: java eclipse eclipse-rcp


【解决方案1】:

我通过传递 shell 而不是显示解决了这个问题。我仍然找不到合适的 swt 文档来说明为什么它有效,但无论如何,这就是我的解决方案现在的样子:

public class PreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

  AuthenticationManager authenticationManager = new AuthenticationManager();

  @Override
  protected void createFieldEditors() {
    final Button login = new Button(getFieldEditorParent(), SWT.PUSH);
    login.setText("Login")
    login.addSelectionListener(new SelectionListener() {
      @Override
      public void widgetSelected(final SelectionEvent se) {
        authenticationManager.run(getShell());
      }
    }
  }
}

public AuthenticationManager {
  public void run(@Nullable Shell optionalShell) {
    final Display display;
    if (optionalShell == null) {
        if (PlatformUI.isWorkbenchRunning()) {
            display = PlatformUI.getWorkbench().getDisplay();
        } else {
            display = null;
        }
    } else {
        display = optionalShell.getDisplay();
    }


    display.syncExec(() -> {
      // set the url & add listeners
    }
  }
}

【讨论】:

  • 大概您正在使用 Shell 作为您创建的对话框的父级。这是因为首选项对话框在其样式中指定了SWT.APPLICATION_MODAL - 只有以应用程序模式对话框为其父级的对话框才能获得焦点。
猜你喜欢
  • 1970-01-01
  • 2015-10-26
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多