Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 14:21 Go to next message
Katarina Olsson is currently offline Katarina OlssonFriend
Messages: 3
Registered: March 2010
Junior Member
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 14:31]

Report message to a moderator

Re: Canvas and FocusListener [message #519655 is a reply to message #519624] Tue, 09 March 2010 10:40 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2151
Registered: July 2009
Senior Member
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 18:07 Go to previous message
Katarina Olsson is currently offline Katarina OlssonFriend
Messages: 3
Registered: March 2010
Junior Member
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 18:09]

Report message to a moderator

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


Current Time: Sat Aug 24 12:03:37 GMT 2019

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

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

Back to the top