Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Problem to give back focus to the main shell
Problem to give back focus to the main shell [message #465131] Tue, 06 December 2005 15:16 Go to next message
hortiz Mising name is currently offline hortiz Mising nameFriend
Messages: 96
Registered: July 2009
Member
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();
}
}
Re: Problem to give back focus to the main shell [message #465142 is a reply to message #465131] Tue, 06 December 2005 17:06 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: davidyoung_2001.yahoo.co.uk

You do get focus on the original shell but you have opened the second
shell with SWT.ON_TOP
Re: Problem to give back focus to the main shell [message #465158 is a reply to message #465142] Wed, 07 December 2005 08:33 Go to previous messageGo to next message
hortiz Mising name is currently offline hortiz Mising nameFriend
Messages: 96
Registered: July 2009
Member
But I want the new shell to be on top, and in any case, removing
SWT.ON_TOP doesn't change anything...

Maybe to be more precise, I work with Eclipse 3.2 on Linux.




david young wrote:

> You do get focus on the original shell but you have opened the second
> shell with SWT.ON_TOP
Re: Problem to give back focus to the main shell [message #465293 is a reply to message #465158] Thu, 08 December 2005 15:45 Go to previous messageGo to next message
james is currently offline jamesFriend
Messages: 3
Registered: July 2009
Junior Member
I had same problem. I tried setFocus and forceFocus but didn't manage to
resolve the problem. In the end I put an identical listener on the new
window but I would prefer a solution where I could put the focus back on
the main window. I'd be interested in a 'cleaner' solution...

James



hortiz wrote:

> But I want the new shell to be on top, and in any case, removing
> SWT.ON_TOP doesn't change anything...

> Maybe to be more precise, I work with Eclipse 3.2 on Linux.




> david young wrote:

>> You do get focus on the original shell but you have opened the second
>> shell with SWT.ON_TOP
Re: Problem to give back focus to the main shell [message #465307 is a reply to message #465131] Thu, 08 December 2005 21:30 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
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();
}
}
});

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(); - this takes focus in newShell - BAD!
newShell.setVisible(true); // this does not take focus in
newShell - GOOD!
}
}


"hortiz" <hortiz@xxxxxxxxxx.xxx> wrote in message
news:701ca867df5061d71192f8a95bc60a28$1@www.eclipse.org...
> 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();
> }
> }
>
>
>
>
Re: Problem to give back focus to the main shell [message #465339 is a reply to message #465307] Fri, 09 December 2005 08:25 Go to previous messageGo to next message
hortiz Mising name is currently offline hortiz Mising nameFriend
Messages: 96
Registered: July 2009
Member
Thanks a lot Veronika, it works fine now !
Re: Problem to give back focus to the main shell [message #465904 is a reply to message #465339] Wed, 21 December 2005 17:04 Go to previous message
Jim Adams is currently offline Jim AdamsFriend
Messages: 160
Registered: July 2009
Senior Member
I have found a similar issue. However, using win.setVisible(true) vs win.open() doesn't change anything. The dialog still has modal control of Eclipse. In my case I am opening a dialog with an embedded browser. I want the dialog to be beside the parent dialog and above it in Z-order. I want to be able to click in it but it must be non-modal. This works just fine on windows but not on linux with firefox embedded in the dialog.
The follwoing bit of code opens the window:
win = new Shell(this.getShell(), SWT.TOOL);
setWindowSize(NOVICE_WINDOW_WIDTH); // simple setBounds() call
win.setLayout(new FillLayout());
noviceArea = new Composite(win, SWT.NULL);
noviceArea.setBackground(win.getDisplay().getSystemColor(SWT .COLOR_INFO_BACKGROUND));
noviceAreaLayout = new StackLayout();
noviceArea.setLayout(noviceAreaLayout);

blankArea = new Composite(noviceArea, SWT.NULL);
blankArea.setBackground(win.getDisplay().getSystemColor(SWT. COLOR_INFO_BACKGROUND));

textArea = new Browser(noviceArea, SWT.NULL);
textArea.setBackground(win.getDisplay().getSystemColor(SWT.C OLOR_INFO_BACKGROUND));

noviceAreaLayout.topControl = blankArea;

setText(defaultText);
open = true;
layout.topControl = leftButton;
leftButton.setEnabled(false);
layout();

win.setVisible(true); // use of .open() causes focus problem?
this.setFocus(); // set focus back on the parent dialog

This bit of code is actually in a wizard dialog page within Eclipse.
Previous Topic:Drag and Drop...on tree
Next Topic:Table.getColumns(), what order is returned
Goto Forum:
  


Current Time: Mon Dec 09 04:16:56 GMT 2024

Powered by FUDForum. Page generated in 0.02648 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top