[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools] Re: Keyboard navigation: why is focus not being set on the canvas?

Veronika, there are two classes that I have that inherit from Canvas. 
When a user is navigating using the tab keys, the focus is never set on
either of the canvases.  For example, I have a text field followed by a
canvas, followed by a list.  When the user is in the text box, and hits
"tab," the focus goes straight to the list (skipping the canvas).  I want
it to focus on the canvas before it gets to the list.  That canvas
essentially just draws an Image, and I want to allow the user to scroll
that image using the arrow keys on the keyboard.  I have written a
keylistener that would allow the user to do that.  But I can only set
focus on that canvas right now by clicking on it, and not by using the
"tab" key on the keyboard.  Any ideas about how could I do that?

Thanks for helping me.

Pratik Shah

Veronika_Irvine@xxxxxxx wrote:

> Here is a simple example showing that the Canvas is taking focus when you 
> navigate around with the tab keys.  When the canvas has focus it is red 
> and when it does not have focus it is blue:

> public static void main(String[] args) {
>         final Display display = new Display();
>         Shell shell = new Shell(display);
>         shell.setLayout(new GridLayout());
>         final Canvas canvas = new Canvas(shell, SWT.BORDER);
>         canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
>         canvas.addFocusListener(new FocusAdapter() {
>                 public void focusGained(FocusEvent e) {
>  canvas.setBackground(display.getSystemColor(SWT.COLOR_RED));
>                 }
>                 public void focusLost(FocusEvent e) {
>  canvas.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
>                 }
>         });
>         Button b = new Button(shell, SWT.PUSH);
>         b.setText("a button");
>         shell.open();
>         while (shell != null && !shell.isDisposed()) {
>                 if (!display.readAndDispatch())
>                         display.sleep(); 
>         } 
> }


> Now if you create a child of the canvas, focus will no longer go to the 
> canvas but to the child of the canvas as is shown in the following simple 
> example.  Notice that the canvas does not change color and that the focus 
> rectangle appears on the button inside the canvas:

> public static void main(String[] args) {
>         final Display display = new Display();
>         Shell shell = new Shell(display);
>         shell.setLayout(new GridLayout());
>         final Canvas canvas = new Canvas(shell, SWT.BORDER);
>         canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
>         canvas.addFocusListener(new FocusAdapter() {
>                 public void focusGained(FocusEvent e) {
>  canvas.setBackground(display.getSystemColor(SWT.COLOR_RED));
>                 }
>                 public void focusLost(FocusEvent e) {
>  canvas.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
>                 }
>         });
>         Button canvasButton = new Button(canvas, SWT.PUSH);
>         canvasButton.setText("a button on the canvas");
>         canvasButton.setBounds(10, 10, 200, 50);

>         Button b = new Button(shell, SWT.PUSH);
>         b.setText("a button");
>         shell.open();
>         while (shell != null && !shell.isDisposed()) {
>                 if (!display.readAndDispatch())
>                         display.sleep(); 
>         } 
> }


> You should not be overriding the setFocus() method on the Canvas as this 
> is only called when the focus changes due to an application 
> programmatically calling setFocus().  It is not called when the focus 
> changes due to a user action.

> If your canvas has children, the focus goes to the children and not to the 
> canvas.  If this is what is happening in your case, why do you want the 
> focus to go the canvas and not to the children?