[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Problem to give back focus to the main shell

Hi,

I have a problem to give back the focus to my main shell after triggering the creation of a new shell.

The snippet below illustrates the problem: when clicking on F1, a new shell is opened, but I can't manage to force the focus back onto the main shell so that F1 creates yet another shell (other than clicking on the main shell before).

How can I do this ?
Thanks,
Helene

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class SetFocusProblem {

	private static Shell shell;
	private static int shellNumbers = 0;
	
   /**
    * @param args
    */
   public static void main(String[] args) {

Display display = new Display();
shell = new Shell (display);
shell.setLayout(new GridLayout());
shell.open();
shell.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent event) {
if (event.keyCode == SWT.F1) {
System.out.println("Opening new shell");
openNewShell();
shell.setFocus(); // does not work ?
}
}
});
while (!shell.isDisposed ()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose ();
}
protected static void openNewShell() {
Shell newShell = new Shell(shell, SWT.ON_TOP);
newShell.setLayout(new GridLayout());
Label label = new Label(newShell, SWT.NONE);
label.setText("new shell " + shellNumbers++);
label.setLayoutData(new GridData());
newShell.setSize(100, 100);
newShell.open();
}
}