[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?

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?