Skip to main content

Listening for MouseEvent outside of a Node

3 replies [Last post]
rperfect
Offline
Joined: 2005-07-12

How do I listen and catch MouseEvents outside of a Node? The following code example works okay if the user clicks inside of the circle, but what happens if you want to find out where they clicked outside of the Circle??

Neither the Frame nor the Stage objects seem to have the ability to catch MouseEvents :-(

Frame {

stage : Stage {
content : [
Circle{
centerX: 50;
centerY: 50;
radius: 25;
fill: Color.BLACK;

onMouseClicked: function(event : MouseEvent) : Void {
System.out.println("MouseEvent = ({event.getX()},{event.getY()})");
}
}
]
}

visible : true
width : 400
height : 300
closeAction : function() { java.lang.System.exit( 0 ); }
}

Also - how do I format code in this forum?? (apologies for the mess above).

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rperfect
Offline
Joined: 2005-07-12

Yes - using a Rectangle as to catch the MouseEvents works. You don't even have to make it the owner of the other nodes. See the code below.

Also in the code below see how you can control whether or not the topmost node blocks the mouse event from being handed onto the rectangle with the "blocksMouse" attribute which is something I noticed in another thread while searching for this problem.

Thanks for the help :-)

[code]
Frame {

stage : Stage {
content : [
Rectangle {
width: 400
height : 300
fill : Color.GRAY

onMouseClicked: function(event : MouseEvent) : Void {
System.out.println("Outer MouseEvent = ({event.getX()},{event.getY()})");
}
},

Circle{
centerX: 50;
centerY: 50;
radius: 25;
fill: Color.BLACK;
blocksMouse: true;

onMouseClicked: function(event : MouseEvent) : Void {
System.out.println("Inner MouseEvent = ({event.getX()},{event.getY()})");
}
}
]
}

visible : true
width : 400
height : 300
closeAction : function() { java.lang.System.exit( 0 ); }
}
[/code]

kram
Offline
Joined: 2007-10-23

You could fill the whole scene with a rectangle, then have add the event to the rectangle. All nodes on the Stage would then be children of the Rectangle.

It would be nice if some of the javafx.application.* objects could handle mouse events.

In regards to code formatting, just use [ code ] and [ /code ] (without spaces)

marykayet
Offline
Joined: 2011-05-06

I really hope this will be solve in no time. I know a lot of developers are tuning in to the solution. I'm pretty sure that when they know about the solution. They'll be able to solve a lot of problems.
coffee makers using capsules