setOnMouseClick JavaFx Region not called


Question

I have a Region-object inside of a Parent for that I want to set the mouseClick-method like this:

item.getNode().setOnMousePressed(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent me) {
                        // do something
                    }
                });

The item is derived from Parent and is created like this:

public MyItem(Region node) {                        
    init(node);  
}

private void init(Node node) {
   getChildren().addAll(node);
}

The problem is that my method handle is never called when I add a Region-object. If I add an ImageView-object the click is received.

Any ideas?

1
3
3/1/2012 10:18:20 PM

Parent usually calculates it's size according to it's children. Empty parent has size 0.

Take a look at the next example. Note that parent size was set manually and "click" output happens only if you click inside pane (red square):

@Override
public void start(Stage stage) throws Exception {

    Pane parent = new Pane();
    parent.setStyle("-fx-border-color:red;"); // to see pane's size visually
    parent.setOnMouseClicked(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent t) {
            System.out.println("click");
        }
    });
    parent.setPrefSize(100, 100); // set size
    stage.setScene(new Scene(new Group(parent),300,300));

    stage.show();
}
4
3/3/2012 8:47:48 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon