Java Swing Mouse convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)

Here you can find the source of convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)

Description

convert Mouse Event

License

Open Source License

Declaration

public static MouseEvent convertMouseEvent(Component source,
            MouseEvent sourceEvent, Component destination) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;
import java.awt.Point;

import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import javax.swing.SwingUtilities;
import javax.swing.event.MenuDragMouseEvent;

public class Main {
    public static MouseEvent convertMouseEvent(Component source,
            MouseEvent sourceEvent, Component destination) {
        Point p = SwingUtilities.convertPoint(source,
                new Point(sourceEvent.getX(), sourceEvent.getY()),
                destination);// ww  w.  j a  v a2s .  co m
        Component newSource;

        if (destination != null)
            newSource = destination;
        else
            newSource = source;

        MouseEvent newEvent;
        if (sourceEvent instanceof MouseWheelEvent) {
            MouseWheelEvent sourceWheelEvent = (MouseWheelEvent) sourceEvent;
            newEvent = new MouseWheelEvent(newSource,
                    sourceWheelEvent.getID(), sourceWheelEvent.getWhen(),
                    sourceEvent.getModifiers()
                            | sourceEvent.getModifiersEx(), p.x, p.y,
                    sourceWheelEvent.getClickCount(),
                    sourceWheelEvent.isPopupTrigger(),
                    sourceWheelEvent.getScrollType(),
                    sourceWheelEvent.getScrollAmount(),
                    sourceWheelEvent.getWheelRotation());
        } else if (sourceEvent instanceof MenuDragMouseEvent) {
            MenuDragMouseEvent sourceMenuDragEvent = (MenuDragMouseEvent) sourceEvent;
            newEvent = new MenuDragMouseEvent(newSource,
                    sourceMenuDragEvent.getID(),
                    sourceMenuDragEvent.getWhen(),
                    sourceEvent.getModifiers()
                            | sourceEvent.getModifiersEx(), p.x, p.y,
                    sourceMenuDragEvent.getClickCount(),
                    sourceMenuDragEvent.isPopupTrigger(),
                    sourceMenuDragEvent.getPath(),
                    sourceMenuDragEvent.getMenuSelectionManager());
        } else {
            newEvent = new MouseEvent(newSource, sourceEvent.getID(),
                    sourceEvent.getWhen(), sourceEvent.getModifiers()
                            | sourceEvent.getModifiersEx(), p.x, p.y,
                    sourceEvent.getClickCount(),
                    sourceEvent.isPopupTrigger(), sourceEvent.getButton());
        }
        return newEvent;
    }
}

Related

  1. addMouseListenerToAll(Component parent, MouseListener listener)
  2. addMouseListenerToHierarchy(JComponent c, MouseListener listener)
  3. animateMouse(Robot robot, JComponent jc, boolean moveMouse, boolean clickOnArrival)
  4. clickedInSelection(MouseEvent e)
  5. convert(MouseEvent event, Component newSource)
  6. convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
  7. convertMouseEvent(java.awt.Component source, java.awt.event.MouseEvent sourceEvent, java.awt.Component destination)
  8. convertPoint(MouseEvent evt, Component target)
  9. createButton(JComponent parent, String description, MouseListener listener)