Biscuit128
Biscuit128

Reputation: 5398

Open window on mouse co-ordinates

I was wondering if it was possible to open a window where the mouse currently is? I have the current mouse co-ordinate but am unable to find what to do with the x y values when displaying my window.

Hopefully someone could point me in the direction of the appropriate method.

Thanks

Upvotes: 2

Views: 1144

Answers (2)

Marcello Marianetti
Marcello Marianetti

Reputation: 21

user1181445 was right, but I think you can set the frame location without creating the variables x and y:

Point location = MouseInfo.getPointerInfo().getLocation(); 
JFrame frame = new JFrame(); //this is just the initialization of the window
frame.setLocation(location);

Upvotes: 0

user1181445
user1181445

Reputation:

If you haven't already, using the MouseInfo class will get the x and y position.

Point location = MouseInfo.getPointerInfo().getLocation(); 

You specified that you want to use a JFrame in this case, so setting the location of the JFrame to this x and y point will do so.

Point location = MouseInfo.getPointerInfo().getLocation(); 
int x = (int) location.getX();
int y = (int) location.getY();
JFrame frame = new JFrame(); //this is just the initialization of the window
frame.setLocation(x, y);

Upvotes: 1

Related Questions