Skip to main content



      Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Canvas and FocusListener(How do I obtain a proper FocusIn and FocusOut behavior in a canvas widget?)
Canvas and FocusListener [message #519624] Tue, 09 March 2010 09:21 Go to next message
Eclipse UserFriend
I have created a custom widget that extends Canvas and implements a FocusListener. The idea is to draw something (in this case a small circle) when the canvas is in focus, and to have the drawing removed when focus is lost (e.g. when another widget is focused). In order to see the widget, I have used a standard widget launcher inside a main method, as described in the eclipse cheat sheets.

Problem is, that the focus is set initially to the first widget, as the shell is opened, but after that, the focuslistener does not seem to respond to e.g. clicks on the second widget in the shell. I have tried other widgets, such as Button, and it works fine. Maybe I have missed or misunderstood something about the behaviour or usage of FocusListener for Canvas? If somebody could give me a hint, that would make my day.


public class TestCanvas extends Canvas implements PaintListener, FocusListener, DisposeListener {

private boolean selected = false;
private Color color = new Color(null, 255, 255, 255);

public TestCanvas(Composite parent, int style) {
super(parent, style);
setBackground(color);
addFocusListener(this);
addPaintListener(this);
}

@Override
public void paintControl(PaintEvent arg0) {
GC gc = arg0.gc;
gc.drawOval(4, 4, 40, 40);
if (selected) {
gc.drawOval(4, 4, 20, 20);
}

}

@Override
public void focusGained(FocusEvent arg0) {
this.selected = true;
redraw();

}

@Override
public void focusLost(FocusEvent arg0) {
this.selected = false;
redraw();
}

@Override
public void widgetDisposed(DisposeEvent e) {
color.dispose();
}

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
TestCanvas canvas = new TestCanvas(shell, 0);
TestCanvas canvas2 = new TestCanvas(shell, 0);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

}


/Katarina

[Updated on: Tue, 09 March 2010 09:31] by Moderator

Re: Canvas and FocusListener [message #519655 is a reply to message #519624] Tue, 09 March 2010 05:40 Go to previous messageGo to next message
Eclipse UserFriend
Hi, I think
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet21.java?view=co
demonstrates what you want.

Grant


"Katarina" <katarina.l.olsson@gmail.com> wrote in message
news:hn5li1$k2g$1@build.eclipse.org...
> I have created a custom widget that extends Canvas and implements a
FocusListener. The idea is to draw something (in this case a small circle)
when the canvas is in focus, and to have the drawing removed when focus is
lost (e.g. when another widget is focused). In order to see the widget, I
have used a standard widget launcher inside a main method, as described in
the eclipse cheat sheets.
>
> public class TestCanvas extends Canvas implements PaintListener,
FocusListener, DisposeListener {
>
> private boolean selected = false;
> private Color color = new Color(null, 255, 255, 255);
>
> public TestCanvas(Composite parent, int style) {
> super(parent, style);
> setBackground(color);
> addFocusListener(this);
> addPaintListener(this);
> }
>
> @Override
> public void paintControl(PaintEvent arg0) {
> GC gc = arg0.gc;
> gc.drawOval(4, 4, 40, 40);
> if (selected) {
> gc.drawOval(4, 4, 20, 20);
> }
>
> }
>
> @Override
> public void focusGained(FocusEvent arg0) {
> this.selected = true;
> redraw();
>
> }
>
> @Override
> public void focusLost(FocusEvent arg0) {
> this.selected = false;
> redraw();
> }
>
> @Override
> public void widgetDisposed(DisposeEvent e) {
> color.dispose();
> }
>
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new RowLayout());
> TestCanvas canvas = new TestCanvas(shell, 0);
> TestCanvas canvas2 = new TestCanvas(shell, 0);
> shell.pack();
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) {
> display.sleep();
> }
> }
> display.dispose();
> }
>
> }
>
> Problem is, that the focus is set initially to the first widget, as the
shell is opened, but after that, the focuslistener does not seem to respond
to e.g. clicks on the second widget in the shell. I have tried other
widgets, such as Button, and it works fine. Maybe I have missed or
misunderstood something about the behaviour or usage of FocusListener for
Canvas? If somebody could give me a hint, I would be really happy.
>
> /Katarina
Re: Canvas and FocusListener [message #519712 is a reply to message #519655] Tue, 09 March 2010 13:07 Go to previous message
Eclipse UserFriend
Grant Gayed wrote on Tue, 09 March 2010 05:40
Hi, I think
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet21.java?view=co
demonstrates what you want.

Grant




Awesome!

That pretty much solved my problem. Thanks a lot!!! Smile

[Updated on: Tue, 09 March 2010 13:09] by Moderator

Previous Topic:SWT syncexec question
Next Topic:SWT: Dependencies of Feature Project
Goto Forum:
  


Current Time: Thu Jul 03 09:04:34 EDT 2025

Powered by FUDForum. Page generated in 0.11087 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top