Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 20:27 Go to next message
Jonah Bron is currently offline Jonah BronFriend
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 #640570 is a reply to message #640271] Mon, 22 November 2010 14:12 Go to previous messageGo to next message
Lakshmi P ShanmugamFriend
Messages: 279
Registered: July 2009
Location: India
Senior Member
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.


Lakshmi P Shanmugam
Re: Mouse Event Zones On Canvas [message #640662 is a reply to message #640271] Mon, 22 November 2010 17:57 Go to previous messageGo to next message
Jonah Bron is currently offline Jonah BronFriend
Messages: 32
Registered: October 2010
Location: California
Member
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 12:24 Go to previous messageGo to next message
Lakshmi P ShanmugamFriend
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

Re: Mouse Event Zones On Canvas [message #640969 is a reply to message #640271] Tue, 23 November 2010 17:30 Go to previous message
Jonah Bron is currently offline Jonah BronFriend
Messages: 32
Registered: October 2010
Location: California
Member
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 Apr 24 19:27:29 GMT 2024

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

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

Back to the top