How to make a SWT Composite selectable?

1.2k views Asked by At

I'm creating a wizard page in eclipse using SWT libraries. So far I have succeeded in creating the page but with only one issue:- SWT composite doesn't get selected.
My wizard page contains a scrolledComposite containing multiple composites in a grid fashion. Each composite contains a browser just like a sample template. What I want is when I select a composite containing browser it should get selected and the finish button activated.

Now, my doubt is, how do I make the composite containing the browser selectable as a composite doesn't contain selectionListener. Or better still, is there a way to make a browser selectable?

Sample code would be appreciated.

I'm a noob and this is my first question on SO so please forgive me for any mistakes in question.

2

There are 2 answers

11
greg-449 On BEST ANSWER

You probably want to use the addFocusListener method of the browser to listen for the control gaining (or losing) the focus. You can then enable/disable the finish button.

The focus listener looks like:

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

  public void focusLost(FocusEvent event)
  {
  }
}

Test program:

public class Stackoverflow
{
  public static void main(final String args [])
  {
    Display.setAppName("Stackoverflow");
    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();
  }
}
0
Saifuddin Hitawala On

Thanks a lot @greg-449 for pushing me in the right direction and being patient with me. After doing some research, I found there was a system specific bug due to which it wouldn't reach the FocusListener code. See bug here https://bugs.eclipse.org/bugs/show_bug.cgi?id=84532

The following code contains workaround through which a SWT Browser can get focus.

public static void main (String [] args) {

    Display.setAppName("Stackoverflow");
    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();

}

Now the browser does get focus through which my button gets activated but is there any way to show it as selected?