Skip to main content



      Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Mouse Event Zones On Canvas(How are active interactible areas created on a canvas?)
Mouse Event Zones On Canvas [message #640271] Fri, 19 November 2010 15:27 Go to next message
Eclipse UserFriend
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: Wed, 24 November 2010 19:47] by Moderator

Re: Mouse Event Zones On Canvas [message #640570 is a reply to message #640271] Mon, 22 November 2010 09:12 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

I didn't quite follow what kind of tool you are looking for?
You can do it using code -- add a listener to the Canvas to get the mouse events. The event's x and y fields are the coordinates of the mouse pointer and can help you determine if the mouse is within your area.
Re: Mouse Event Zones On Canvas [message #640662 is a reply to message #640271] Mon, 22 November 2010 12:57 Go to previous messageGo to next message
Eclipse UserFriend
Yeah. I mean something like adding a listener for, say, a Rectangle (object). It doesn't look like that's a built-in feature, though.
Re: Mouse Event Zones On Canvas [message #640876 is a reply to message #640662] Tue, 23 November 2010 07:24 Go to previous messageGo to next message
Eclipse UserFriend
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.

[Updated on: Tue, 23 November 2010 07:25] by Moderator

Re: Mouse Event Zones On Canvas [message #640969 is a reply to message #640271] Tue, 23 November 2010 12:30 Go to previous message
Eclipse UserFriend
Oh cool, I wasn't aware of Rectangle.contains. That will make this much easier. Thanks Very Happy
Previous Topic:How associate a combo in a TableItem in a Table
Next Topic:Long cells in table/tree - truncate from left?
Goto Forum:
  


Current Time: Wed Jul 23 16:20:59 EDT 2025

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

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

Back to the top