Hi, Manual.
There are several things you need to do to add focus capability to a Canvas.
- make absolutely sure that you do NOT create it with NO_FOCUS style.
- yes, as you discovered, you need to hook a key listener for the canvas to
take focus
- you also need to have a traverse listener, and set doit to true when the
event detail is TRAVERSE_TAB_NEXT/PREVIOUS, so that the user can traverse
out of the canvas using the keyboard
- you will probably want to set a caret into the canvas using something like
the following:
Caret caret = new Caret(canvas, SWT.NONE);
Font font = canvas.getFont();
caret.setFont(font);
GC gc = new GC(canvas);
gc.setFont(font);
caret.setBounds(1, 1, 1, gc.getFontMetrics().getHeight()); // so that
the caret height matches the text height
gc.dispose();
canvas.setCaret (caret);
- you can set focus to the canvas using canvas.setFocus();
If you do all of this, there shouldn't be any problem with scrollbars and
focus - it should all just work automatically.
Hope this helps,
Carolyn
"Manuel Selva" <
manuel.selva@xxxxxxxx> wrote in message
news:57a4e87972361a2777f021b6d3c7304a$
1@xxxxxxxxe.org...
Hi Grant,
In fact i still have a question about focus management and key events ...
After looking into the SWT's code i concluded this :
- Widget can have focus only if there is one KeyListener hooked on it. Am
i right ?
Now i want one of my composite (a Canvas) to have focus on it when the
user click in the canvas.
To do this i have 2 solutions :
- Add an empty KeyListener to this composite() and SWT will set focus on
it each time the user clicks the Canvas.
- Add a MouseListener to my canvas and force focus each time the user
clicks by calling the setFocus() method on my canvas.
Is there any solution better than the other one ??
Moreover my Canvas is created using the SWT.H_SCROLL and SWT.V_SCROLL
styles and i noticed that my Canvas is not focused each time the user uses
scrollbars. To do this i have to add a selection listener on my
scrollbars and to force focus by calling setFocus on the canvas each time
the user uses it. Shouldn't SWT automatically set focus of the underlying
widget of the scrollbar ?
Thanks
Manu
http://manuelselva.wordpress.com/