Example usage for java.awt.event MouseEvent getX

List of usage examples for java.awt.event MouseEvent getX

Introduction

In this page you can find the example usage for java.awt.event MouseEvent getX.

Prototype

public int getX() 

Source Link

Document

Returns the horizontal x position of the event relative to the source component.

Usage

From source file:Main.java

/**
 * Gives a component a popup menu//w w w.j ava2  s. com
 *
 * @param component
 *            The target component
 * @param popup
 *            The popup menu
 */

public static void setPopupMenu(final JComponent component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(final MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.show(component, e.getX(), e.getY());
            }
        }

        @Override
        public void mouseReleased(final MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.show(component, e.getX(), e.getY());
            }
        }
    });
}

From source file:Main.java

/**
 * Attach popup menu on the given component.
 * //from  w w  w  .ja v a2  s. c om
 * @param component
 *            component to which the popupMenu is attached
 * @param popupMenu
 *            popupMenu to be attached
 */
public static void attachPopupMenu(final JComponent component, final JPopupMenu popupMenu) {
    component.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
                popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        }

    });
}

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 .j av  a2 s .c  o  m
            }
        }

        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:com.googlecode.blaisemath.app.MenuConfig.java

private static JButton popupButton(String name, final JPopupMenu popup) {
    final JButton button = new JButton(name);
    button.addMouseListener(new MouseAdapter() {
        @Override//from  w  w  w.  j a  v  a2  s  .  c  o m
        public void mousePressed(MouseEvent e) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    });
    return button;
}

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;/*  w ww  .  j  a v a 2 s  .c o  m*/
                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:de.ailis.xadrian.utils.SwingUtils.java

/**
 * Gives a component a popup menu//from w  w w .java 2  s.  c o  m
 * 
 * @param component
 *            The target component
 * @param popup
 *            The popup menu
 */
public static void setPopupMenu(final JComponent component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(final MouseEvent e) {
            // Ignore mouse buttons outside of the normal range. This
            // fixes problems with trackpad scrolling.
            if (e.getButton() > MouseEvent.BUTTON3)
                return;

            if (e.isPopupTrigger()) {
                popup.show(component, e.getX(), e.getY());
            }
        }

        @Override
        public void mouseReleased(final MouseEvent e) {
            // Ignore mouse buttons outside of the normal range. This
            // fixes problems with trackpad scrolling.
            if (e.getButton() > MouseEvent.BUTTON3)
                return;

            if (e.isPopupTrigger()) {
                popup.show(component, e.getX(), e.getY());
            }
        }
    });
}

From source file:Main.java

public void mouseDragged(MouseEvent e) {
    imageX = e.getX();
    imageY = e.getY();
    repaint();
}

From source file:MainClass.java

public MainClass() {
    setLayout(null);//from   w  ww  . ja va 2 s.co  m
    add(button);
    button.setSize(button.getPreferredSize());
    button.setLocation(20, 20);
    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            button.setLocation(e.getX(), e.getY());
        }
    });
}

From source file:Main.java

public Main() {
    setLayout(null);//from   w  w  w .ja v a 2  s  .co m
    add(button);
    button.setSize(button.getPreferredSize());
    button.setLocation(20, 20);
    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            button.setLocation(e.getX(), e.getY());
        }
    });
}

From source file:XORModePaintWithMouse.java

XORModePaintWithMouse() {
    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseMoved(MouseEvent me) {
            int x = me.getX();
            int y = me.getY();
            chsX = x - 10;/*from  ww w .j  a  va2  s . c  om*/
            chsY = y - 10;
            repaint();
        }
    });
}