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);
}
};
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?