26

I'm doing some Swing GUI work with Java, and I think my question is fairly straightforward; How does one set the position of the mouse?

5 Answers 5

28

As others have said, this can be achieved using Robot.mouseMove(x,y). However this solution has a downfall when working in a multi-monitor situation, as the robot works with the coordinate system of the primary screen, unless you specify otherwise.

Here is a solution that allows you to pass any point based global screen coordinates:

public void moveMouse(Point p) {
    GraphicsEnvironment ge = 
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();

    // Search the devices for the one that draws the specified point.
    for (GraphicsDevice device: gs) { 
        GraphicsConfiguration[] configurations =
            device.getConfigurations();
        for (GraphicsConfiguration config: configurations) {
            Rectangle bounds = config.getBounds();
            if(bounds.contains(p)) {
                // Set point to screen coordinates.
                Point b = bounds.getLocation(); 
                Point s = new Point(p.x - b.x, p.y - b.y);

                try {
                    Robot r = new Robot(device);
                    r.mouseMove(s.x, s.y);
                } catch (AWTException e) {
                    e.printStackTrace();
                }

                return;
            }
        }
    }
    // Couldn't move to the point, it may be off screen.
    return;
}
1
  • 3
    @Daniel--it's been years since you replied here, but today I found it and it has solved a problem I'd been having for many months with inadvertent mouse clicks repositioning the text cursor. I was tempted to just use Robot.mouseMove until I read your comment. Strangely, I had just begun to use a multi-monitor setup! Since you use classes that I'm not familiary with, I was shocked that all I had to do was copy your code, do a few imports, and call it. Good job! Thanks!!
    – DSlomer64
    Feb 17, 2015 at 18:15
23

You need to use Robot

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.

Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events...

0
8

Robot.mouseMove(x,y)

2

Check out the Robot class.

0

The code itself is the following:

char escCode = 0x1B;
System.out.print(String.format("%c[%d;%df",escCode,row,column));

This code is incomplete by itself, so I recommend placing it in a method and calling it something like 'positionCursor(int row, int column)'.

Here is the code in full (method and code):

void positionCursor(int row, int column) {
        char escCode = 0x1B;
        System.out.print(String.format("%c[%d;%df",escCode,row,column));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.