Java Utililty Methods Swing Mouse

List of utility methods to do Swing Mouse

Description

The list of methods to do Swing Mouse are organized into topic(s).

Method

MouseEventadaptEventToDescendent(MouseEvent e, JComponent descendentTarget)
adapt Event To Descendent
Point trans = new Point();
Component source = e.getComponent();
Component current = descendentTarget;
while (current != source) {
    Rectangle b = current.getBounds();
    trans.x += b.x;
    trans.y += b.y;
    current = current.getParent();
...
voidaddMouseListenerToAll(Component parent, MouseListener listener)
add Mouse Listener To All
if (parent instanceof AbstractButton) {
    AbstractButton a = (AbstractButton) parent;
    boolean is = false;
    MouseListener[] kl = a.getMouseListeners();
    for (MouseListener k : kl) {
        if (k.equals(listener)) {
            is = true;
            break;
...
voidaddMouseListenerToHierarchy(JComponent c, MouseListener listener)
add Mouse Listener To Hierarchy
for (Component comp : c.getComponents()) {
    comp.addMouseListener(listener);
    if (comp instanceof JComponent) {
        addMouseListenerToHierarchy((JComponent) comp, listener);
voidanimateMouse(Robot robot, JComponent jc, boolean moveMouse, boolean clickOnArrival)
Move the mouse to the center of the component provided.
Point p = new Point(jc.getWidth() / 2, jc.getHeight() / 2);
animateMouse(robot, jc, p, moveMouse, clickOnArrival ? 1 : 0);
booleanclickedInSelection(MouseEvent e)
Tests if one clicked on already selected rows.
if (!(e.getSource() instanceof JTable))
    return false;
JTable invoker = (JTable) e.getSource();
if (invoker.getSelectedRows().length == 0)
    return false;
int clickedRow = invoker.rowAtPoint(e.getPoint());
for (int selectedRow : invoker.getSelectedRows()) {
    if (selectedRow == clickedRow)
...
MouseEventconvert(MouseEvent event, Component newSource)
convert
return convert(event, newSource,
        SwingUtilities.convertPoint((Component) event.getSource(), event.getPoint(), newSource));
MouseEventconvertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
Convert a mouse event which refrers to one component to another.
Point newpt = convertPoint(source, sourceEvent.getX(), sourceEvent.getY(), destination);
return new MouseEvent(destination, sourceEvent.getID(), sourceEvent.getWhen(), sourceEvent.getModifiers(),
        newpt.x, newpt.y, sourceEvent.getClickCount(), sourceEvent.isPopupTrigger(),
        sourceEvent.getButton());
MouseEventconvertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
convert Mouse Event
Point p = SwingUtilities.convertPoint(source, new Point(sourceEvent.getX(), sourceEvent.getY()),
        destination);
Component newSource;
if (destination != null)
    newSource = destination;
else
    newSource = source;
MouseEvent newEvent;
...
java.awt.event.MouseEventconvertMouseEvent(java.awt.Component source, java.awt.event.MouseEvent sourceEvent, java.awt.Component destination)
convert Mouse Event
int modifiers = sourceEvent.getModifiers();
int modifiersEx = sourceEvent.getModifiersEx();
int modifiersComplete = modifiers | modifiersEx;
java.awt.event.MouseEvent me = javax.swing.SwingUtilities.convertMouseEvent(source, sourceEvent,
        destination);
if (me instanceof java.awt.event.MouseWheelEvent) {
    java.awt.event.MouseWheelEvent mwe = (java.awt.event.MouseWheelEvent) me;
    return new java.awt.event.MouseWheelEvent(me.getComponent(), me.getID(), me.getWhen(),
...
PointconvertPoint(MouseEvent evt, Component target)
convert Point
return SwingUtilities.convertPoint(evt.getComponent(), evt.getPoint(), target);