[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 that does just that.

I suspect that your problem is that instead of letting the canvas create 
its own scrollbars, you are creating separate Slider objects and the focus 
is going to the sliders because now the canvas has children.  Is that 
possible?

Just use the style bits SWT.H_SCROLL and SWT._SCROLL when creating the 
canvas and all the work of creating and positioning the scroll bars will 
be done for you.

public static void main(String[] args) {
        final Display display = new Display();
        final Image image = new Image(display, _Scrapbook.class.getResourceAsStream ("aLargePicture.bmp"));
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
 
        Text text = new Text(shell, SWT.MULTI | SWT.BORDER);
 
        final Canvas canvas = new Canvas(shell, SWT.BORDER | SWT.NO_BACKGROUND | SWT.V_SCROLL | 
SWT.H_SCROLL);
        canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
        Rectangle imageBounds = image.getBounds();
        canvas.setSize(imageBounds.width, imageBounds.height);
        canvas.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e){
                        Rectangle imageBounds = image.getBounds();
                        ScrollBar vbar = canvas.getVerticalBar();
                        ScrollBar hbar = canvas.getHorizontalBar();
                        int x = hbar.getSelection();
                        int y = vbar.getSelection();
                        e.gc.drawImage(image, 0 - x, 0 - y);
                }
        });
        canvas.addKeyListener(new KeyAdapter() {
                public void keyReleased(KeyEvent e) {
                        if (e.keyCode == SWT.ARROW_UP) {
                                System.out.println("key up");
                                ScrollBar bar = canvas.getVerticalBar();
                                bar.setSelection(bar.getSelection() - 1);
                                canvas.redraw();
                        }
                        if (e.keyCode == SWT.ARROW_DOWN) {
                                System.out.println("key down");
                                ScrollBar bar = canvas.getVerticalBar();
                                bar.setSelection(bar.getSelection() + 1);
                                canvas.redraw();
                        }
                        if (e.keyCode == SWT.ARROW_LEFT) {
                                System.out.println("key left");
                                ScrollBar bar = canvas.getHorizontalBar();
                                bar.setSelection(bar.getSelection() - 1);
                                canvas.redraw();
                        }
                        if (e.keyCode == SWT.ARROW_RIGHT) {
                                System.out.println("key right");
                                ScrollBar bar = canvas.getHorizontalBar();
                                bar.setSelection(bar.getSelection() + 1);
                                canvas.redraw();
                        }
                }
        });
 
        List list = new List(shell, SWT.BORDER);
        list.setItems(new String[]{"item 1", "item 2", "item 3", "item 4"});
 
        shell.open();
        while (shell != null && !shell.isDisposed()) {
                if (!display.readAndDispatch())
                        display.sleep(); 
        } 
}