Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 so please forgive me for any mistakes in question.
Posted

1 solution

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.

Java
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?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900