Example usage for java.awt Component addMouseMotionListener

List of usage examples for java.awt Component addMouseMotionListener

Introduction

In this page you can find the example usage for java.awt Component addMouseMotionListener.

Prototype

public synchronized void addMouseMotionListener(MouseMotionListener l) 

Source Link

Document

Adds the specified mouse motion listener to receive mouse motion events from this component.

Usage

From source file:Main.java

/**
 * Inserts the mouse motion listener at the particular index in the listeners' chain.
 *
 * @param component/*from  w  w  w .j a  v a 2  s .  c o m*/
 * @param l
 * @param index
 */
public static void insertMouseMotionListener(Component component, MouseMotionListener l, int index) {
    MouseMotionListener[] listeners = component.getMouseMotionListeners();

    for (MouseMotionListener listener : listeners) {
        component.removeMouseMotionListener(listener);
    }

    for (int i = 0; i < listeners.length; i++) {
        MouseMotionListener listener = listeners[i];

        if (index == i) {
            component.addMouseMotionListener(l);
        }

        component.addMouseMotionListener(listener);
    }

    // inex is too large, add to the end.
    if (index > listeners.length - 1) {
        component.addMouseMotionListener(l);
    }
}

From source file:Main.java

private static void addComponentListeners(Component c, Object... objs) {
    if (c == null)
        return;//from w  w w . j  av  a 2  s .  c  o  m
    ComponentListener componentListener = search(objs, ComponentListener.class);
    FocusListener focusListener = search(objs, FocusListener.class);
    HierarchyBoundsListener hierarchyBoundsListener = search(objs, HierarchyBoundsListener.class);
    HierarchyListener hierarchyListener = search(objs, HierarchyListener.class);
    InputMethodListener inputMethodListener = search(objs, InputMethodListener.class);
    KeyListener keyListener = search(objs, KeyListener.class);
    MouseListener mouseListener = search(objs, MouseListener.class);
    MouseMotionListener mouseMotionListener = search(objs, MouseMotionListener.class);
    MouseWheelListener mouseWheelListener = search(objs, MouseWheelListener.class);
    if (componentListener != null)
        c.addComponentListener(componentListener);
    if (focusListener != null)
        c.addFocusListener(focusListener);
    if (hierarchyBoundsListener != null)
        c.addHierarchyBoundsListener(hierarchyBoundsListener);
    if (hierarchyListener != null)
        c.addHierarchyListener(hierarchyListener);
    if (inputMethodListener != null)
        c.addInputMethodListener(inputMethodListener);
    if (keyListener != null)
        c.addKeyListener(keyListener);
    if (mouseListener != null)
        c.addMouseListener(mouseListener);
    if (mouseMotionListener != null)
        c.addMouseMotionListener(mouseMotionListener);
    if (mouseWheelListener != null)
        c.addMouseWheelListener(mouseWheelListener);
}

From source file:Main.java

public static void addMiddleButtonDragSupport(Component targetComponent) {
    MouseInputAdapter mia = new MouseInputAdapter() {
        int m_XDifference, m_YDifference;
        boolean m_dragging = false;

        public void mouseDragged(MouseEvent e) {
            if (!m_dragging)
                return;

            Component target = e.getComponent();
            Container c = target.getParent();
            if (c instanceof JViewport) {
                JViewport jv = (JViewport) c;
                Point p = jv.getViewPosition();
                int newX = p.x - (e.getX() - m_XDifference);
                int newY = p.y - (e.getY() - m_YDifference);

                int maxX = target.getWidth() - jv.getWidth();
                int maxY = target.getHeight() - jv.getHeight();
                if (newX < 0)
                    newX = 0;/*from   ww  w.  j  av a  2s .  c om*/
                if (newX > maxX)
                    newX = maxX;
                if (newY < 0)
                    newY = 0;
                if (newY > maxY)
                    newY = maxY;

                jv.setViewPosition(new Point(newX, newY));
            }
        }

        Cursor oldCursor;

        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isMiddleMouseButton(e)) {
                m_dragging = true;
                oldCursor = e.getComponent().getCursor();
                e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                m_XDifference = e.getX();
                m_YDifference = e.getY();
            }
        }

        public void mouseReleased(MouseEvent e) {
            if (m_dragging) {
                e.getComponent().setCursor(oldCursor);
                m_dragging = false;
            }
        }
    };
    targetComponent.addMouseMotionListener(mia);
    targetComponent.addMouseListener(mia);
}

From source file:com.mac.tarchan.desktop.event.EventQuery.java

/**
 * ??????????/*  w  w  w  .  j  ava  2s  . c  o  m*/
 * 
 * @param target ?
 * @param action ?????????????
 * @param property ???????????? 
 * @return ??
 * @see MouseListener#mousePressed(java.awt.event.MouseEvent)
 * @see MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
 */
public EventQuery swipe(Object target, String action, String property) {
    //      MouseListener mousePressed = EventHandler.create(MouseListener.class, target, action, property, "mousePressed");
    MouseMotionListener mouseDragged = EventHandler.create(MouseMotionListener.class, target, action, property,
            "mouseDragged");
    MouseSwipeHandler swipeHandler = new MouseSwipeHandler();
    for (Component child : list) {
        //         child.addMouseListener(mousePressed);
        child.addMouseMotionListener(mouseDragged);
        child.addMouseListener(swipeHandler);
        child.addMouseMotionListener(swipeHandler);
    }

    return this;
}

From source file:Filter3dTest.java

/**
 * Creates a new InputManager that listens to input from the specified
 * component.//from   w ww.  java  2 s .  c  o  m
 */
public InputManager(Component comp) {
    this.comp = comp;
    mouseLocation = new Point();
    centerLocation = new Point();

    // register key and mouse listeners
    comp.addKeyListener(this);
    comp.addMouseListener(this);
    comp.addMouseMotionListener(this);
    comp.addMouseWheelListener(this);

    // allow input of the TAB key and other keys normally
    // used for focus traversal
    comp.setFocusTraversalKeysEnabled(false);
}