SetOnMouseMoved() not working after SetOnMousePressed()

I have a button when I click it runs a function. I also have a separate SetOnMouseMoved event to get information when I hover over other cells in my grid example. My SetOnMouseMoved event will not work after I have pressed the setOnMouseClicked.

   Button btn = new Button("Uniform Path");
   GridPane gp = new GridPane();
   gp.add(btn, 0,1)

   btn.setOnMouseClicked(new EventHandler<MouseEvent> (){
        @Override 
        public void handle(MouseEvent e) {
             SomeFunction();  
        }
    });

    // gp.getChildren() refers to StackPane grid cells in which I
    // hover over and get information from that cell.    
    for (Node index : gp.getChildren()) {
        index.setOnMouseMoved(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.print("Row: " + GridPane.getRowIndex(index));
                System.out.println("Column: " + GridPane.getColumnIndex(index));
            }
        });
    }

   Scene scene = new Scene(gp);

1 answer

  • answered 2017-11-13 15:04 F. Kopzhassarov

    From docs.oracle.com:

    setOnMouseMoved: Defines a function to be called when mouse cursor moves within this Node but no buttons have been pushed.

    setOnMouseDragged: Defines a function to be called when a mouse button is pressed on this Node and then dragged.

    You just need to implement setOnMouseDragged the same as setOnMouseMoved.