如何使 SWT Composite 可选择?

How to make a SWT Composite selectable?

我正在使用 SWT 库在 Eclipse 中创建一个向导页面。到目前为止,我已经成功创建了页面,但只有一个问题:- SWT composite doesn't get selected.
我的向导页面包含一个 scrolledComposite,其中包含多个网格形式的复合材料。 每个组合都包含一个浏览器,就像一个示例模板。我想要的是当我 select 一个包含浏览器的组合时,它应该得到 selected 并且激活完成按钮。

现在,我的疑问是,如何使包含浏览器的组合 select 可用,因为组合不包含 selectionListener。或者更好的是,有没有办法让浏览器 select 可用?

示例代码将不胜感激。

我是菜鸟,这是我关于 SO 的第一个问题,所以如果有任何错误,请原谅我。

您可能想使用浏览器的 addFocusListener 方法来监听控件获得(或失去)焦点。然后您可以 enable/disable 完成按钮。

焦点侦听器看起来像:

public class MyFocusListener implements FocusListener
{
  public void focusGained(FocusEvent event)
  {
  }

  public void focusLost(FocusEvent event)
  {
  }
}

测试程序:

public class Whosebug
{
  public static void main(final String args [])
  {
    Display.setAppName("Whosebug");
    final Display display = new Display();
    final Shell shell = new Shell(display);

    shell.setSize(500, 400);
    shell.setLayout(new FillLayout());

    final Composite composite = new Composite(shell, SWT.BORDER);
    composite.setLayout(new GridLayout());

    final Browser browser = new Browser(composite, SWT.NONE);
    browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final Button finish = new Button(composite, SWT.PUSH);
    finish.setText("Finish");
    finish.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));

    browser.setUrl("google.com");

    final FocusListener listener = new FocusListener()
      {
        public void focusLost(final FocusEvent arg0)
        {
        }


        public void focusGained(final FocusEvent arg0)
        {
          System.out.println("FocusGained");
          finish.setEnabled(true);
        }
      };
    browser.addFocusListener(listener);

    shell.open();

    while (!shell.isDisposed())
      {
        if (!display.readAndDispatch())
          display.sleep();
      }
    display.dispose();
  }
}

非常感谢@greg-449 将我推向正确的方向并对我耐心等待。在做了一些研究之后,我发现有一个系统特定的错误,导致它无法到达 FocusListener 代码。请在此处查看错误 https://bugs.eclipse.org/bugs/show_bug.cgi?id=84532

以下代码包含解决方法,SWT 浏览器可通过该方法获取焦点。

public static void main (String [] args) {

    Display.setAppName("Whosebug");
    final Display display = new Display();
    final Shell shell = new Shell(display);

    shell.setSize(500, 400);
    shell.setLayout(new FillLayout());

    final Composite composite = new Composite(shell, SWT.BORDER);
    composite.setLayout(new GridLayout());

    final Browser browser = new Browser(composite, SWT.NONE);
    browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final Button finish = new Button(shell, SWT.PUSH);
    finish.setText("Finish");
    finish.setEnabled(false);
    finish.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));

    browser.setUrl("google.com");

    final FocusListener listener = new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            System.out.println("Focus lost");
        }

        @Override
        public void focusGained(FocusEvent e) {
            System.out.println("Focus gained");
            finish.setEnabled(true);
        }
    };

    /*
     * workaround for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=84532
     */
    Listener deactivateListener = new Listener() {

        @Override
        public void handleEvent(Event event) {
            listener.focusLost(new FocusEvent(event));
        }
    };
    Listener activateListener = new Listener() {

        @Override
        public void handleEvent(Event event) {
            listener.focusGained(new FocusEvent(event));
        }
    };

    browser.addListener(SWT.Deactivate, deactivateListener);
    browser.addListener(SWT.Activate, activateListener);

    finish.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e){
            System.out.println("OK");
        }
    });

    shell.open();

    while (!shell.isDisposed())
      {
        if (!display.readAndDispatch())
          display.sleep();
      }
    display.dispose();

}

现在浏览器确实获得了焦点,通过它激活了我的按钮,但是有什么方法可以将其显示为选中状态吗?