【问题标题】:setFocus() always falsesetFocus() 总是假的
【发布时间】:2012-11-01 00:51:05
【问题描述】:

我正在开发一个 Eclipse RCP 应用程序。在扩展 MultiPageEditorPart 的类中,我试图将焦点设置到文本字段。但是 setFocus 方法总是返回 false。

我做错了什么?

MultiPageEditor 有各种页面,在这些页面中,有 Composite - 类。这些类包含文本字段。

这里是 sn-p:(errorPage 是一个 int,我的验证发现错误的页码)

if(!dataValid) {
   MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Fehler bei der Dateneingabe", stringBuilder.toString());
   this.setActivePage(errorPage);
   Composite errorComposite = (Composite) this.getControl(errorPage);
   Control[] children = errorComposite.getChildren();
   for (Control child : children) {
    if(child instanceof Form) {
     Form form = (Form) child;
     Composite body = form.getBody();
     Control[] formChildren = body.getChildren();
     for (Control formChild : formChildren) {
      if(formChild.equals(errorControl)) 
                            formChild.setFocus();
      return dataValid;
     } 
    }
   }
  }

【问题讨论】:

    标签: java eclipse user-interface swt rcp


    【解决方案1】:

    setFocus() 可能在以下情况下返回 false:

    1. 也许控件是像 Label 这样无法聚焦的控件
    2. 复合型尝试在自己集中注意力之前先将注意力分配给孩子
    3. 如果控件被禁用或隐藏,它将不会获得焦点
    4. 输入因模式而被阻止。

    所以我最好检查一下,(1)我是否将焦点设置在正确的控件上,(2)控件是否可见,可能包含该控件的表单不在当前选定的选项卡中。 (3) 是否打开了任何其他模式对话框。

    【讨论】:

    • 也许您在Shell 可见之前调用了setFocus()
    【解决方案2】:

    你试过 Control#forceFocus() 吗?

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 2019-10-11
      • 2020-10-13
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-29
      相关资源
      最近更新 更多