Skip to main content

Input event propagation

2 replies [Last post]
Joined: 2008-10-03

I'm having troubles with mouse event propagation. I made a simple test app to try dragging rectangles. For some reason, if I drag an object on top of another, then click and drag it again, then both objects under the pointer get dragged. How do I stop event propagation when the top object gets hit? (I'm new to this, so I'm likely missing something simple.)

Here's the code if you're curious, Note that I had to cast to a rectangle in the handlers, which makes them less portable.

package dragvbox;

import javafx.application.Frame;
import javafx.application.Stage;
import javafx.scene.geometry.Rectangle;
import javafx.scene.paint.Color;
import javafx.input.MouseEvent;
import javafx.scene.Group;

//vertical dragging behavior
class RectDragY{
attribute startY = 0.0;
public function onPress(mEvt:MouseEvent): Void {
startY = (mEvt.node as Rectangle).y
public function onDrag(mEvt:MouseEvent): Void {
(mEvt.node as Rectangle).y = startY + mEvt.getDragY()

var rectDragY = RectDragY{};

Frame {
title: "DragTest"
width: 200; height: 200
closeAction: function() { java.lang.System.exit( 0 ); }
visible: true
stage: Stage {
Group {
var colors = [Color.RED, Color.YELLOW, Color.CYAN,Color.GREEN, Color.BLUE ];
var pos = -10;
content: [
for (color in colors)
Rectangle {
fill:color, width:100, height:10, y: pos += 15;
onMouseDragged: rectDragY.onDrag,
onMousePressed: rectDragY.onPress
}] } } }

Reply viewing options

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

The Node class has an attribute called "blocksMouse" - if you set that to true then the topmost element with blocksMouse = true gets to process the event and then the event goes no further.

Also note that in order to receive mouse events your Node/Shape must be "filled". If the Node has no fill then the user needs to click on the stroked outline (which may or may not be want you want of course sometimes).

Joined: 2008-10-03

Hey thanks - that fixed it!

I had cribbed the dragging code from a few web sites, and they had left that bit out. (Of course now that I google "blocksMouse getDragX" I find that I could chosen better examples. [b]:[/b] )

Thanks again,