Project: SWT
Bug 60334 Can't force focus to a shell (BBAWT)
Reported: 2004-04-28 17:15:12 | Status: resolved fixed

I have a situation where I have my own "lightweight" widgets that are drawn within a real SWT widget. Since in SWT, *something* must have focus, I set the real focus to the lightweight widget's parent. When that parent is a shell, and I call forceFocus() on the shell, the window manager (or someone) is being "helpful" by moving the focus to the first focusable child. In this case, I really wanted the shell to be the focus widget. Here's a testcase that illustrates what I'm trying to do. When the shell opens, the text widget is given focus. Then, when I force the focus to be the shell, I don't expect the button to then be given focus, especially since I have a traverse listener on the shell that sets the "doit" to false. Shouldn't the traverse listener allow me to prevent the button from receiving focus? If not, how can I do this? -- testcase -- import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.events.*; //------------------------------------------------------------------- // //------------------------------------------------------------------- public class TestSwtForceFocus { private Display display; private Shell shell; //------------------------------------------------------------------- // //------------------------------------------------------------------- static public void main(String[] args) { new TestSwtForceFocus().runMain(args); } //------------------------------------------------------------------- // //------------------------------------------------------------------- private void runMain(String[] args) { display = new Display(); shell = new Shell(display); shell.setText(getClass().getName()); shell.setLayout(new GridLayout()); shell.addShellListener(new ShellAdapter() { public void shellActivated(ShellEvent e) { System.out.println("shell activated"); } }); Button push = new Button(shell, 0); push.setText("push"); Text text = new Text(shell, 0); shell.setSize(300, 300); shell.addTraverseListener(new TraverseListener() { public void keyTraversed(TraverseEvent e) { e.doit = false; } }); shell.open(); text.forceFocus(); shell.forceFocus(); System.out.println( "button is focus control = " + push.isFocusControl()); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } }

Files changed in fix:
bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Decorations.java