setOnMouseClick JavaFx Region not called


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>() {
                    public void handle(MouseEvent me) {
                        // do something

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

public MyItem(Region node) {                        

private void init(Node 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?

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):

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>() {

        public void handle(MouseEvent t) {
    parent.setPrefSize(100, 100); // set size
    stage.setScene(new Scene(new Group(parent),300,300));;
3/3/2012 8:47:48 AM

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