[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?
|
- From: pashah@xxxxxxxxxx (Pratik Shah)
- Date: 19 Apr 2001 22:03:56 GMT
- Newsgroups: eclipse.tools
- Organization: http://www.eclipsecorner.org
- User-agent: NewsPortal 0.23
Nope, that's not it. I am using SWT.H_SCROLL and SWT.V_SCROLL style bits
to create the scroll bars.
Do you know what is it that makes the focus go to a canvas? Or any other
widget for that matter? Initially, I was thinking that maybe a canvas
gets no focus while tabbing because it inherits from Composite. And a
composite gets no focus. Only its children do. Maybe the method that
enables focusing was overridden in Composite. Then again, what is it that
makes a label not get focus? This is just some random thinking on my part.
Maybe I can put a large button or something on the canvas (making it the
same size as the canvas) and then draw the image on there. The button
will most certainly get focus then. On second thought, I don't like this
hackish idea.
Then again, I am using a version of SWT that is a few months old. Maybe
focus is being set on a canvas in the latest version of SWT.
Thanks for trying to help, Veronica. But I guess I am still where I
started.
Pratik Shah
Veronika_Irvine@xxxxxxx wrote:
> 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();
> }
> }