Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » calling setFocus from a listener for activate event
calling setFocus from a listener for activate event [message #452364] Fri, 18 March 2005 04:00 Go to next message
Francis Leboutte is currently offline Francis Leboutte
Messages: 19
Registered: July 2009
Junior Member
In a listener for an activate event (Ca control), I'm trying to set the
focus to another control (Cb), using setFocus. This doesn't work. For
example, if I use the Tab to activate Ca, the focus goes into a third
control (the one that follows Ca). If I use the mouse and click into
control Ca, the cursor and focus just stay in Ca.

Maybe I should remove any pending events before to call setFocus from
the listener. Is this feasible?

Thank you for any hints.

Francis
Re: calling setFocus from a listener for activate event [message #452410 is a reply to message #452364] Sun, 20 March 2005 08:02 Go to previous messageGo to next message
Eclipse User
Originally posted by: hoeggerand.post.ch

Hi Francis
use a traverse listener on ca which sets the focus to cb.
cheers siambalam

Francis Leboutte wrote:
> In a listener for an activate event (Ca control), I'm trying to set the
> focus to another control (Cb), using setFocus. This doesn't work. For
> example, if I use the Tab to activate Ca, the focus goes into a third
> control (the one that follows Ca). If I use the mouse and click into
> control Ca, the cursor and focus just stay in Ca.
>
> Maybe I should remove any pending events before to call setFocus from
> the listener. Is this feasible?
>
> Thank you for any hints.
>
> Francis
Re: calling setFocus from a listener for activate event [message #452481 is a reply to message #452410] Mon, 21 March 2005 05:03 Go to previous messageGo to next message
Francis Leboutte is currently offline Francis Leboutte
Messages: 19
Registered: July 2009
Junior Member
Siambalam a écrit :
> Hi Francis
> use a traverse listener on ca which sets the focus to cb.

Hello,

What I want is to is to set the focus to Cb control whenever the Ca
control is activated (by tab, by mouse click,...)

I have not tested your suggestion, possibly it will work for traversal
but that would be funny: why would it work from a traverse listener and
not from a activate listener?

Francis

> cheers siambalam
>
> Francis Leboutte wrote:
>
>> In a listener for an activate event (Ca control), I'm trying to set
>> the focus to another control (Cb), using setFocus. This doesn't work.
>> For example, if I use the Tab to activate Ca, the focus goes into a
>> third control (the one that follows Ca). If I use the mouse and click
>> into control Ca, the cursor and focus just stay in Ca.
>>
>> Maybe I should remove any pending events before to call setFocus from
>> the listener. Is this feasible?
>>
>> Thank you for any hints.
>>
>> Francis
Re: calling setFocus from a listener for activate event [message #452492 is a reply to message #452364] Mon, 21 March 2005 09:53 Go to previous messageGo to next message
Grant Gayed is currently offline Grant Gayed
Messages: 2148
Registered: July 2009
Senior Member
Francis,

The Active event indicates that a Shell has become not active, not that a
widget has gained focus. You need to use a focus listener, as shown in the
snippet below which moves focus off of text2 whenever the user tries to put
it there:

public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell(display);
shell.setBounds(10,10,200,200);
final Text text1 = new Text(shell, SWT.SINGLE);
text1.setBounds(10,10,50,30);
Text text2 = new Text(shell, SWT.SINGLE);
text2.setBounds(10,50,50,30);
text2.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
text1.setFocus();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}

Grant

"Francis Leboutte" <f.leboutte@algo.be> wrote in message
news:d1e5c1$a0k$1@www.eclipse.org...
> In a listener for an activate event (Ca control), I'm trying to set the
> focus to another control (Cb), using setFocus. This doesn't work. For
> example, if I use the Tab to activate Ca, the focus goes into a third
> control (the one that follows Ca). If I use the mouse and click into
> control Ca, the cursor and focus just stay in Ca.
>
> Maybe I should remove any pending events before to call setFocus from
> the listener. Is this feasible?
>
> Thank you for any hints.
>
> Francis
Re: calling setFocus from a listener for activate event [message #452504 is a reply to message #452492] Mon, 21 March 2005 12:00 Go to previous messageGo to next message
Francis Leboutte is currently offline Francis Leboutte
Messages: 19
Registered: July 2009
Junior Member
Grant Gayed a écrit :
> Francis,
>
> The Active event indicates that a Shell has become not active, not that a
> widget has gained focus.

From what I can see, in my context, each time a widget gains focus
using the mouse or the tab, there are a focusin and a activate events,
plus a mousedown and selection (mouse only) or a traverse event (tab).
There are still a focusin and activate events when the user switch back
to the application (Alt-tab on Windows).

This is why I was trying to use the activate event for my purpose.

Actually I'm writing a tool for Common Lisp developers that should help
them to build dialogs using SWT. It would have been more simple, for me
at least, to use the activate event, not touching the focusin and out
events.

Thank you Grant for the example,

Francis

PS
I'm new to Java and SWT. In the example below, I see you set the size of
the text widgets using setBounds. I have just tried to use setSize and
computeSize on text control within a layout, without success (no bug,
but no effect). Is there something special I should know about these
methods?


You need to use a focus listener, as shown in the
> snippet below which moves focus off of text2 whenever the user tries to put
> it there:
>
> public static void main(String[] args) {
> Display display = new Display ();
> Shell shell = new Shell(display);
> shell.setBounds(10,10,200,200);
> final Text text1 = new Text(shell, SWT.SINGLE);
> text1.setBounds(10,10,50,30);
> Text text2 = new Text(shell, SWT.SINGLE);
> text2.setBounds(10,50,50,30);
> text2.addFocusListener(new FocusAdapter() {
> public void focusGained(FocusEvent e) {
> text1.setFocus();
> }
> });
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();
> }
>
> Grant
>
> "Francis Leboutte" <f.leboutte@algo.be> wrote in message
> news:d1e5c1$a0k$1@www.eclipse.org...
>
>>In a listener for an activate event (Ca control), I'm trying to set the
>>focus to another control (Cb), using setFocus. This doesn't work. For
>>example, if I use the Tab to activate Ca, the focus goes into a third
>>control (the one that follows Ca). If I use the mouse and click into
>>control Ca, the cursor and focus just stay in Ca.
>>
>>Maybe I should remove any pending events before to call setFocus from
>>the listener. Is this feasible?
>>
>>Thank you for any hints.
>>
>>Francis
>
>
>
Re: calling setFocus from a listener for activate event [message #452555 is a reply to message #452504] Tue, 22 March 2005 04:19 Go to previous messageGo to next message
Francis Leboutte is currently offline Francis Leboutte
Messages: 19
Registered: July 2009
Junior Member
Francis Leboutte a écrit :
....
>
> PS
> I'm new to Java and SWT. In the example below, I see you set the size of
> the text widgets using setBounds. I have just tried to use setSize and
> computeSize on text control within a layout, without success (no bug,
> but no effect). Is there something special I should know about these
> methods?

I have found what I should have known: the layout algorithm discards any
initial and previous sizing information. Thanks to possibly comment on
the rest.

....
Re: calling setFocus from a listener for activate event [message #452858 is a reply to message #452492] Fri, 25 March 2005 03:00 Go to previous messageGo to next message
Francis Leboutte is currently offline Francis Leboutte
Messages: 19
Registered: July 2009
Junior Member
Grant Gayed a écrit :
> Francis,
>
> The Active event indicates that a Shell has become not active, not that a
> widget has gained focus. You need to use a focus listener, as shown in the
> snippet below which moves focus off of text2 whenever the user tries to put
> it there:

Using a focus listener works, but only if the user uses the mouse or the
return key (default button), not if he uses the tab key. To fix this, I
have tried to flush the event queue (display.readAndDispatch) before the
call to setFocus, without success. Thanks,

Francis

>
> public static void main(String[] args) {
> Display display = new Display ();
> Shell shell = new Shell(display);
> shell.setBounds(10,10,200,200);
> final Text text1 = new Text(shell, SWT.SINGLE);
> text1.setBounds(10,10,50,30);
> Text text2 = new Text(shell, SWT.SINGLE);
> text2.setBounds(10,50,50,30);
> text2.addFocusListener(new FocusAdapter() {
> public void focusGained(FocusEvent e) {
> text1.setFocus();
> }
> });
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();
> }
>
> Grant
>
> "Francis Leboutte" <f.leboutte@algo.be> wrote in message
> news:d1e5c1$a0k$1@www.eclipse.org...
>
>>In a listener for an activate event (Ca control), I'm trying to set the
>>focus to another control (Cb), using setFocus. This doesn't work. For
>>example, if I use the Tab to activate Ca, the focus goes into a third
>>control (the one that follows Ca). If I use the mouse and click into
>>control Ca, the cursor and focus just stay in Ca.
>>
>>Maybe I should remove any pending events before to call setFocus from
>>the listener. Is this feasible?
>>
>>Thank you for any hints.
>>
>>Francis
>
>
>
Re: calling setFocus from a listener for activate event [message #452867 is a reply to message #452858] Fri, 25 March 2005 13:22 Go to previous messageGo to next message
Eclipse User
Originally posted by: bob.objfac.com

I wonder why a focus listener doesn't work with the tab key? I know you
can see this with a traversal listener, but I wonder why you have to?
The control does gain/lose focus.

Bob Foster

Francis Leboutte wrote:
> Grant Gayed a écrit :
>
>> Francis,
>>
>> The Active event indicates that a Shell has become not active, not that a
>> widget has gained focus. You need to use a focus listener, as shown
>> in the
>> snippet below which moves focus off of text2 whenever the user tries
>> to put
>> it there:
>
>
> Using a focus listener works, but only if the user uses the mouse or the
> return key (default button), not if he uses the tab key. To fix this, I
> have tried to flush the event queue (display.readAndDispatch) before the
> call to setFocus, without success. Thanks,
>
> Francis
>
>>
>> public static void main(String[] args) {
>> Display display = new Display ();
>> Shell shell = new Shell(display);
>> shell.setBounds(10,10,200,200);
>> final Text text1 = new Text(shell, SWT.SINGLE);
>> text1.setBounds(10,10,50,30);
>> Text text2 = new Text(shell, SWT.SINGLE);
>> text2.setBounds(10,50,50,30);
>> text2.addFocusListener(new FocusAdapter() {
>> public void focusGained(FocusEvent e) {
>> text1.setFocus();
>> }
>> });
>> shell.open();
>> while (!shell.isDisposed()) {
>> if (!display.readAndDispatch()) display.sleep();
>> }
>> display.dispose();
>> }
>>
>> Grant
>>
>> "Francis Leboutte" <f.leboutte@algo.be> wrote in message
>> news:d1e5c1$a0k$1@www.eclipse.org...
>>
>>> In a listener for an activate event (Ca control), I'm trying to set the
>>> focus to another control (Cb), using setFocus. This doesn't work. For
>>> example, if I use the Tab to activate Ca, the focus goes into a third
>>> control (the one that follows Ca). If I use the mouse and click into
>>> control Ca, the cursor and focus just stay in Ca.
>>>
>>> Maybe I should remove any pending events before to call setFocus from
>>> the listener. Is this feasible?
>>>
>>> Thank you for any hints.
>>>
>>> Francis
>>
>>
>>
>>
Re: calling setFocus from a listener for activate event [message #452869 is a reply to message #452867] Fri, 25 March 2005 13:41 Go to previous message
Francis Leboutte is currently offline Francis Leboutte
Messages: 19
Registered: July 2009
Junior Member
Bob Foster a écrit :
> I wonder why a focus listener doesn't work with the tab key? I know you
> can see this with a traversal listener, but I wonder why you have to?
> The control does gain/lose focus.

It "works", but the focus finally is gained by another control (not the
one specified in the call to setFocus).

>
> Bob Foster
>
> Francis Leboutte wrote:
>
>> Grant Gayed a écrit :
>>
>>> Francis,
>>>
>>> The Active event indicates that a Shell has become not active, not
>>> that a
>>> widget has gained focus. You need to use a focus listener, as shown
>>> in the
>>> snippet below which moves focus off of text2 whenever the user tries
>>> to put
>>> it there:
>>
>>
>>
>> Using a focus listener works, but only if the user uses the mouse or
>> the return key (default button), not if he uses the tab key. To fix
>> this, I have tried to flush the event queue (display.readAndDispatch)
>> before the call to setFocus, without success. Thanks,
>>
>> Francis
>>
>>>
>>> public static void main(String[] args) {
>>> Display display = new Display ();
>>> Shell shell = new Shell(display);
>>> shell.setBounds(10,10,200,200);
>>> final Text text1 = new Text(shell, SWT.SINGLE);
>>> text1.setBounds(10,10,50,30);
>>> Text text2 = new Text(shell, SWT.SINGLE);
>>> text2.setBounds(10,50,50,30);
>>> text2.addFocusListener(new FocusAdapter() {
>>> public void focusGained(FocusEvent e) {
>>> text1.setFocus();
>>> }
>>> });
>>> shell.open();
>>> while (!shell.isDisposed()) {
>>> if (!display.readAndDispatch()) display.sleep();
>>> }
>>> display.dispose();
>>> }
>>>
>>> Grant
>>>
>>> "Francis Leboutte" <f.leboutte@algo.be> wrote in message
>>> news:d1e5c1$a0k$1@www.eclipse.org...
>>>
>>>> In a listener for an activate event (Ca control), I'm trying to set the
>>>> focus to another control (Cb), using setFocus. This doesn't work. For
>>>> example, if I use the Tab to activate Ca, the focus goes into a third
>>>> control (the one that follows Ca). If I use the mouse and click into
>>>> control Ca, the cursor and focus just stay in Ca.
>>>>
>>>> Maybe I should remove any pending events before to call setFocus from
>>>> the listener. Is this feasible?
>>>>
>>>> Thank you for any hints.
>>>>
>>>> Francis
>>>
>>>
>>>
>>>
>>>
Previous Topic:howto create context menu in Motif Eclipse?
Next Topic:Spinners?
Goto Forum:
  


Current Time: Thu Jul 24 23:41:47 EDT 2014

Powered by FUDForum. Page generated in 0.12355 seconds