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?
You should use Component.getLocationOnScreen() instead. This method returns absolute coordinates.