Skip to content
Advertisement

Retrieve mouse position in JavaFX without event

I want to retrieve the x and y coordinates of the current mouse position before showing a stage.

So far, the only way I found to get the mouse position in JavaFX is within a MouseEvent, which does not apply to my situation. Furthermore, I found the possibility to retrieve the position via java.awt.MouseInfo. This, however, I think is a bad idea (I use JavaFX not AWT) and, at least in my case, results in a HeadlessException.

Is there an other clean possibility to retrieve the mouse position in JavaFX without getting too hackish (e.g. simulating a MouseEvent just for getting the position)?

Thank you very much!

Advertisement

Answer

Well, you could get mouse coordinates using Robot class. Here is an example.

JavaScript

It tried it on linux (elementary OS) and it works.

Update: After some googling, I found TestFX which looks like an attempt to implement a prototype for Robot class. Have a look at links given below. https://github.com/TestFX/Robot http://mail.openjdk.java.net/pipermail/openjfx-dev/2015-December/018412.html

You can also do something like this, to get the coordinates.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement