Getting the mouse coordinates out of Jframe

Asked

Viewed 217 times

-1

I have a Jframe implemented in a simple way with Mousemotionlistener in java to get the coordinates of a variable, and I am returning with gety() and getx(). So far so good, but when I noticed the coordinates shown in the Jframe, are exactly the coordinates from inside the Jframe, when the mouse exceeds the edges of the window the coordinates are no longer rewritten. What procedure or class or medium, that in Java, allows me to "listen outside the frame" the movement of the mouse cursor ?

  • Provide a [mcve] so that we can help you.

  • This is the method I used to catch the movement inside Jframe: Override public void mouseMoved(Mouseevent e) { posicaY.setText(Integer.toString(e.gety())); posicaoX.setText(Integer.toString(e.getx()); }

1 answer

1

According to this reply from Soen, it is possible to get the mouse coordinates, even outside Jframe, with the methods below, originated from the class MouseInfo:

MouseInfo.getPointerInfo().getLocation().x;
MouseInfo.getPointerInfo().getLocation().y;
  • This method has the same function as gety(), and getx, in the java.awt package I wanted to see if there is a way to get it straight from the operating system (if JVM can see it later). But from the past example referring to another topic, the user simulated a window below the screen launched by Jframe, is an interesting technique I will study more about it. Vlw.

  • @Rivaldodossantos I myself tested here and worked normally, it captures exactly the position of the mouse on the screen, regardless if the mouse is on the jframe or not. I think you probably don’t know the API well, awt takes everything from the operating system.

  • Right inside mouseMover implemented so:

  • public void mouseMoved(Mouseevent e) { posicaoY.setText(Integer.toString(Mouseinfo.getPointerInfo(). getLocation(). y)); posicaoX.setText(Integer.toString(Mouseinfo.getPointerInfo(). getLocation(). x));

  • posicaY and posicaoX is a Jlabel, the component you used to view is from the javax.swing package or java.awt ?? How did Voce implement ?

  • @Rivaldodossantos I did exactly as it is in my answer.

  • Ok, more in your method addMotionListener what Voce added in the parameter or in your addMouseEvent??

  • @Rivaldodossantos I repeat, I did exactly as it is in my reply, I did not use any of that that you are talking there. If the answer did not give the solution in exactly the way that helps you, I suggest that Improve the question. This link has guidelines on how to edit and improve it.

  • @Articuno there is some way to talk to you outside the site? ;)

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.