Question

I need to trigger a MouseEvent mouseClicked on a JLabel (or any other component for that matter). How do I do this?

I tried it using the Robot class as follows:

try {
    Robot r=new Robot();
    r.mouseMove(jl.gettX(), jl.getY());//jl is the JLabel
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (AWTException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

but the getX() and getY() are returning the x,y positions of the component's parent. How do I get the absolute position of a component so that I can trigger Robot.mousePress()? Is there any other way to trigger the event on a specific component?

Was it helpful?

Solution

You should use Component.getLocationOnScreen() instead. This method returns absolute coordinates.

OTHER TIPS

I would recommend using FEST-Swing which allows you to write code that looks like this dialog.button("ok").click();

It is normally used for testing Swing components, but actually you can use it at other times too. You may find it useful to set the name of the component so you can find them easily with fest.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top