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

Have you tried my simple example which shows focus going to the canvas?

I have included it here again.  When the canvas has focus, it is red and 
when it does not have focus it is blue.

Focus has always gone to the canvas (even in very old versions of SWT) as 
long as the canvas does not have any children.

What does your subclass of Canvas look like?  Does it create children? 
What methods have you overridden?  How are you determining that focus is 
not going to your Canvas subclass?  Have you tried using a simple print 
statement to show that you are not in the focusGained or focusLost events? 
 Have you tried replacing your subclass of Canvas with just a plain Canvas 
instance to see if that works?

If a widget is created with the style SWT.NO_FOCUS it will not take focus. 
 
If a widget has children it will not take focus. 

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(); 
        } 
}