Mouse Event Zones On Canvas [message #640271] |
Fri, 19 November 2010 20:27 |
Jonah Bron Messages: 32 Registered: October 2010 Location: California |
Member |
|
|
Hello, world!
I'd like to create areas on my canvas in which if hovered over or clicked, something happens. Of course this can be done manually, but I just want to know if SWT provides tools to make this easier before I undertake the venture. There was nothing relevant to this as far as I could see in the canvas Javadoc, and I didn't find anything on Google.
Thanks
[Updated on: Thu, 25 November 2010 00:47] Report message to a moderator
|
|
|
|
|
Re: Mouse Event Zones On Canvas [message #640876 is a reply to message #640662] |
Tue, 23 November 2010 12:24 |
|
Lakshmi P Shanmugam Messages: 279 Registered: July 2009 Location: India |
Senior Member |
|
|
You cannot add listener to the Rectangle, but you can add listeners to the Canvas.
Here is a simple example:
final Canvas canvas = new Canvas(shell, SWT.NONE);
final Rectangle rect = new Rectangle(10, 10, 50, 50);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawRectangle(rect);
}
});
canvas.addMouseTrackListener(new MouseTrackListener() {
public void mouseHover(MouseEvent e) {
if (rect.contains(e.x, e.y)) {
System.out.println("hover inside rectangle");
} else {
System.out.println("hover outside rectangle");
}
}
public void mouseExit(MouseEvent e) {
}
public void mouseEnter(MouseEvent e) {
}
});
You could add the other Mouse listeners similarly.
Lakshmi P Shanmugam
[Updated on: Tue, 23 November 2010 12:25] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03487 seconds