Posted By

dalcacer on 03/09/10


Tagged

MouseEvent


Versions (?)

Simulate mouseevent


 / Published in: Java
 

  1. /**
  2.   * Clicks in a given area of a Swing component.
  3.   */
  4. public static void doClickInRectangle(Component component,
  5. Rectangle rect,
  6. boolean useRightClick,
  7. Key.Modifier keyModifier) {
  8. int modifiers = useRightClick ? MouseEvent.BUTTON3_MASK : MouseEvent.BUTTON1_MASK;
  9. modifiers |= keyModifier.getCode();
  10. final int nbClicks = 1;
  11. final int x = rect.x + (rect.width / 2);
  12. final int y = rect.y + (rect.height / 2);
  13. component.dispatchEvent(new MouseEvent(component, MouseEvent.MOUSE_PRESSED, 1, modifiers, x, y, nbClicks, false));
  14. component.dispatchEvent(new MouseEvent(component, MouseEvent.MOUSE_RELEASED, 1, modifiers, x, y, nbClicks, useRightClick));
  15.  

Report this snippet  

You need to login to post a comment.