Example usage for java.awt Component addMouseListener

List of usage examples for java.awt Component addMouseListener

Introduction

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

Prototype

public synchronized void addMouseListener(MouseListener l) 

Source Link

Document

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

Usage

From source file:Main.java

/**
 * adds the mouse listener to the component and all its children. This is
 * useful when you want to catch events on the ends of a scroll bar.
 * //from w ww.ja v  a2  s . c  o m
 * @param comp
 *            a GUI component (e.g., a scroll bar)
 * @param listener
 *            a mouse listener
 */
public static void registerWithAllChildren(Component comp, MouseListener listener) {
    comp.addMouseListener(listener);

    if (comp instanceof Container)
        for (Component child : ((Container) comp).getComponents())
            registerWithAllChildren(child, listener);
}

From source file:Main.java

public static void addMouseListenerToHierarchy(JComponent c, MouseListener listener) {
    for (Component comp : c.getComponents()) {
        comp.addMouseListener(listener);

        if (comp instanceof JComponent) {
            addMouseListenerToHierarchy((JComponent) comp, listener);
        }/*from w  w  w  . ja  v a  2s .co  m*/
    }
}

From source file:Main.java

public static final void addPopup(final Component c, final JPopupMenu m) {
    c.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger())
                m.show(c, e.getX(), e.getY());
        }/*  www .j  a  va 2 s .  c  om*/
    });
}

From source file:Main.java

/**
 * Adds a MouseListener to the component specified that will show the popup
 * specified (at the position that the mouse was clicked) when the mouse is
 * right-clicked, or whatever mouse event returns true from the
 * {@link MouseEvent#isPopupTrigger()} method.<br/><br/>
 * /*  w  w w  . j a v  a 2s  .co m*/
 * @param c
 *            The component to add the mouse listener to
 * @param popup
 *            the popup to show whe the component is clicked
 */
public static void addPopup(Component c, final JPopupMenu popup) {
    c.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger())
                popup.show(e.getComponent(), e.getX(), e.getY());
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            mousePressed(e);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            mousePressed(e);
        }
    });
}

From source file:Main.java

public static void addPopup(Component component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);//from   w w  w  .  ja v  a 2s .  c  om
            }
        }

        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }

        private void showMenu(MouseEvent e) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    });
}

From source file:Main.java

/**
 * Adds a hand cursor to the component, as well as a click listener that
 * triggers a browse action to the given url.
 *///  w  ww  . j av  a 2s.  c  om
public static void addBrowseBehavior(final Component cmp, final String url) {
    if (url == null)
        return;
    cmp.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    cmp.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            JFrame frame = getJFrame(cmp);
            browse(url, frame);
        }
    });
}

From source file:Main.java

public static void restoreMouseListeners(final Map<Component, List<MouseListener>> map, final JComponent root) {
    for (final Component c : getAllSubComponents(root)) {
        List<MouseListener> listeners = map.get(c);
        if (listeners != null)
            for (final MouseListener ml : map.get(c)) {
                c.addMouseListener(ml);
            }/*from   ww w  . ja va 2  s  .c o m*/
    }
}

From source file:Main.java

/**
 * Adds a hand cursor to the component, as well as a click listener that
 * triggers a browse action to the given url.
 *//*from   w  w w.  j  a  v  a  2  s.  co m*/
public static void addBrowseBehavior(final Component cmp, final String url) {
    if (url == null) {
        return;
    }
    cmp.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    cmp.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            JFrame frame = getJFrame(cmp);
            browse(url, frame);
        }
    });
}

From source file:Main.java

/**
 * Installs the mouse listener in the tree of components where the argument component is included, starting in the
 * first parent <i>JFrame</i> or <i>JDialog</i> parent.
 * // w  w  w . j a  v a2s .  c o m
 * @param cmp The starting components in the tree.
 * @param mouseListener The mouse listener to install.
 * @param removePrevious A boolean indicating whether previous key listeners should be removed.
 */
public static void installMouseListener(Component cmp, MouseListener mouseListener, boolean removePrevious) {
    Component parent = getFirstParentFrameOrDialog(cmp);
    if (parent == null) {
        parent = cmp;
    }
    List<Component> components = getAllComponents(parent);
    for (Component component : components) {
        if (removePrevious) {
            removeMouseListeners(component);
        }
        component.addMouseListener(mouseListener);
    }
}

From source file:Main.java

/**
 * Inserts the mouse listener at the particular index in the listeners' chain.
 *
 * @param component// w  w  w.j a va  2 s. co m
 * @param l
 * @param index
 */
public static void insertMouseListener(Component component, MouseListener l, int index) {
    MouseListener[] listeners = component.getMouseListeners();

    for (MouseListener listener : listeners) {
        component.removeMouseListener(listener);
    }

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

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

        component.addMouseListener(listener);
    }

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