1

I was hoping to achieve this with the changelistener attached to an integer (mouse.x). But the getPointerInfo().getLocation(); is once initialized. After that the position is not updated. How can i make these changelisteners work on a change of a value?

Maybe there is a better way of doing this. Any help is appreciated.

public class TheMidiApp extends Application {  

@Override
public void start(Stage stage) throws Exception {
Point mouse = java.awt.MouseInfo.getPointerInfo().getLocation();             
System.out.println("MouseX " + mouse.x);
System.out.println("MouseY " + mouse.y);

//Changelistener X
IntegerProperty changeValueMouseX = new SimpleIntegerProperty(mouse.x);
changeValueMouseX.addListener(new ChangeListener<Number>(){
    @Override
    public void changed(ObservableValue <? extends Number> observableValue, Number oldValue, Number newValue){
        System.out.println("OLD MOUSE X VALUE: " + oldValue);
        System.out.println("NEW MOUSE X VALUE: " + newValue);
    }
});

//Changelistener Y                    
IntegerProperty changeValueMouseY = new SimpleIntegerProperty(mouse.y); 
changeValueMouseX.addListener(new ChangeListener<Number>(){ 
    @Override
    public void changed(ObservableValue <? extends Number> observableValue, Number oldValue, Number newValue){
        System.out.println("OLD MOUSE Y VALUE: " + oldValue);
        System.out.println("NEW MOUSE Y VALUE: " + newValue);
    }
});

MouseX = mouse.x;
MouseY = mouse.y;
Bram
  • 13
  • 7

1 Answers1

0

You need a listener on the root node.

public class JavaFXApplication29 extends Application {

    @Override
    public void start(Stage primaryStage) {


        AnchorPane root = new AnchorPane();
        Scene scene = new Scene(root, 500, 500);

        root.setOnMouseMoved(new EventHandler<MouseEvent>(){
            @Override
            public void handle(MouseEvent event) {
                System.out.println("mouse X: " + event.getX());
                System.out.println("mouse Y: " + event.getY());
            }
        });       

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
SedJ601
  • 12,173
  • 3
  • 41
  • 59
  • That would be a good start, however I need to know the position outside of the application (Java), because the mouse can be on a other screen. I have a main screen and a touch screen. The app runs on the touchscreen. Iam using this to teleport my mouse back at the original position of were it left of. The teleporting of the mouse position works, only it goes back to the same position every time. That's why i want to keep track and update the old mouse position of the main screen. – Bram Nov 28 '16 at 18:43
  • http://stackoverflow.com/questions/2469515/java-mouse-motion-anywhere-on-screen The answer to this question may help. – SedJ601 Nov 28 '16 at 20:32