Example usage for java.awt.event MouseEvent getButton

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

Introduction

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

Prototype

public int getButton() 

Source Link

Document

Returns which, if any, of the mouse buttons has changed state.

Usage

From source file:de.ailis.xadrian.utils.SwingUtils.java

/**
 * Gives a component a popup menu//from  w w w  .  j ava 2s  . c om
 * 
 * @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 static MouseEvent adaptEventToDescendent(MouseEvent e, JComponent descendentTarget) {
    Point trans = new Point();
    Component source = e.getComponent();

    Component current = descendentTarget;
    while (current != source) {
        Rectangle b = current.getBounds();
        trans.x += b.x;// w ww. ja  v a  2s  .c o m
        trans.y += b.y;
        current = current.getParent();
    }
    Point point = e.getPoint();

    return new MouseEvent(descendentTarget, e.getID(), e.getWhen(), e.getModifiers(), point.x + trans.x,
            point.y + trans.y, e.getClickCount(), e.isPopupTrigger(), e.getButton());
}

From source file:Main.java

public Main() {
    setLayout(null);/*from  ww  w  .jav a  2s. co  m*/
    add(button);
    button.setSize(button.getPreferredSize());
    button.setLocation(20, 20);
    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            System.out.println(e.getButton() == MouseEvent.BUTTON1);
        }
    });
}

From source file:Main.java

public Main() {
    setLayout(null);/*from   w  w w  . j a v  a 2s. co  m*/
    add(button);
    button.setSize(button.getPreferredSize());
    button.setLocation(20, 20);
    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            System.out.println(e.getButton() == MouseEvent.BUTTON2);
        }
    });
}

From source file:Main.java

public Main() {
    setLayout(null);/*w  w w  . ja v a  2 s  .c o  m*/
    add(button);
    button.setSize(button.getPreferredSize());
    button.setLocation(20, 20);
    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            System.out.println(e.getButton() == MouseEvent.BUTTON3);
        }
    });
}

From source file:Main.java

public Main() {
    setLayout(null);/*from w w  w.jav a 2 s .c  om*/
    add(button);
    button.setSize(button.getPreferredSize());
    button.setLocation(20, 20);
    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            System.out.println(e.getButton() == MouseEvent.NOBUTTON);
        }
    });
}

From source file:Main.java

public Main() {
    setSize(300, 300);/*w  w w  .  j av  a  2s .  c o m*/
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTextArea textArea = new JTextArea();
    textArea.setText("Click Me!");

    textArea.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.NOBUTTON) {
                textArea.setText("No button clicked...");
            } else if (e.getButton() == MouseEvent.BUTTON1) {
                textArea.setText("Button 1 clicked...");
            } else if (e.getButton() == MouseEvent.BUTTON2) {
                textArea.setText("Button 2 clicked...");
            } else if (e.getButton() == MouseEvent.BUTTON3) {
                textArea.setText("Button 3 clicked...");
            }

            System.out.println("Number of click: " + e.getClickCount());
            System.out.println("Click position (X, Y):  " + e.getX() + ", " + e.getY());
        }
    });

    getContentPane().add(textArea);
}

From source file:Test.java

public void mousePressed(MouseEvent e) {
    System.out.println("" + e.getButton());

}

From source file:com.db2eshop.gui.component.tab.ArticleTab.java

/**
 * <p>registerMouseListener.</p>
 *
 * @param jScrollPane a {@link javax.swing.JScrollPane} object.
 * @param table a {@link com.db2eshop.gui.component.table.api.GenericTable} object.
 *///  www. ja  va 2s  . c  o  m
public void registerMouseListener(JScrollPane jScrollPane, GenericTable<?> table) {
    jScrollPane.addMouseListener(new BaseMouseListener() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            if (arg0.getButton() == MouseEvent.BUTTON3) {
                tabRightClickPopupMenu.showMenu(arg0.getPoint(), null, null, articleTable);
            }
        }
    });
}

From source file:com.db2eshop.gui.component.tab.ArticleTypeTab.java

/**
 * <p>registerMouseListener.</p>
 *
 * @param jScrollPane a {@link javax.swing.JScrollPane} object.
 * @param table a {@link com.db2eshop.gui.component.table.api.GenericTable} object.
 *//*from  w  ww.  j a v a  2s  .  c  om*/
public void registerMouseListener(JScrollPane jScrollPane, GenericTable<?> table) {
    jScrollPane.addMouseListener(new BaseMouseListener() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            if (arg0.getButton() == MouseEvent.BUTTON3) {
                tabRightClickPopupMenu.showMenu(arg0.getPoint(), null, null, articleTypeTable);
            }
        }
    });
}