Example usage for java.awt.event MouseEvent getModifiers

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

Introduction

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

Prototype

@Deprecated(since = "9")
public int getModifiers() 

Source Link

Document

Returns the modifier mask for this event.

Usage

From source file:Main.java

public static void main(String[] args) {
    String[] items = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
    JList<String> myJList = new JList(items) {
        @Override//from   www . j a v  a 2s. c om
        protected void processMouseEvent(MouseEvent e) {
            int modifiers = e.getModifiers() | InputEvent.CTRL_MASK;
            int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_MASK;
            MouseEvent myME = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), modifiers,
                    e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(),
                    e.isPopupTrigger(), e.getButton());
            super.processMouseEvent(myME);
        }
    };
    JFrame f = new JFrame();
    f.add(new JScrollPane(myJList));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:ButtonSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Select Me");
    // Define MouseListener
    MouseListener mouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            int modifiers = mouseEvent.getModifiers();
            if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
                System.out.println("Left button pressed.");
            }/*  ww w .  j a  va 2 s  .  com*/
            if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
                System.out.println("Middle button pressed.");
            }
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                System.out.println("Right button pressed.");
            }
        }

        public void mouseReleased(MouseEvent mouseEvent) {
            if (SwingUtilities.isLeftMouseButton(mouseEvent)) {
                System.out.println("Left button released.");
            }
            if (SwingUtilities.isMiddleMouseButton(mouseEvent)) {
                System.out.println("Middle button released.");
            }
            if (SwingUtilities.isRightMouseButton(mouseEvent)) {
                System.out.println("Right button released.");
            }
            System.out.println();
        }
    };
    // Attach listeners
    button.addMouseListener(mouseListener);
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Select Me");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("I was selected.");
        }//from   www  .  j  a v a  2s . c o  m
    };

    MouseListener mouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            int modifiers = mouseEvent.getModifiers();
            if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
                System.out.println("Left button pressed.");
            }
            if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
                System.out.println("Middle button pressed.");
            }
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                System.out.println("Right button pressed.");
            }
        }

        public void mouseReleased(MouseEvent mouseEvent) {
            if (SwingUtilities.isLeftMouseButton(mouseEvent)) {
                System.out.println("Left button released.");
            }
            if (SwingUtilities.isMiddleMouseButton(mouseEvent)) {
                System.out.println("Middle button released.");
            }
            if (SwingUtilities.isRightMouseButton(mouseEvent)) {
                System.out.println("Right button released.");
            }
            System.out.println();
        }
    };

    button.addActionListener(actionListener);
    button.addMouseListener(mouseListener);

    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Select Me");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("I was selected.");
        }/* w w  w  .  j  a v  a 2 s. c o m*/
    };

    MouseListener mouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            int modifiers = mouseEvent.getModifiers();
            if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
                // Mask may not be set properly prior to Java 2
                // See SwingUtilities.isLeftMouseButton() for workaround
                System.out.println("Left button pressed.");
            }
            if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
                System.out.println("Middle button pressed.");
            }
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                System.out.println("Right button pressed.");
            }
        }

        public void mouseReleased(MouseEvent mouseEvent) {
            if (SwingUtilities.isLeftMouseButton(mouseEvent)) {
                System.out.println("Left button released.");
            }
            if (SwingUtilities.isMiddleMouseButton(mouseEvent)) {
                System.out.println("Middle button released.");
            }
            if (SwingUtilities.isRightMouseButton(mouseEvent)) {
                System.out.println("Right button released.");
            }
            System.out.println();
        }
    };

    button.addActionListener(actionListener);
    button.addMouseListener(mouseListener);

    Container contentPane = frame.getContentPane();
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JTabbedPane jTabbedPane = new JTabbedPane();
    jTabbedPane.addTab("Red", new JLabel("Roses"));
    jTabbedPane.addTab("Blue", new JLabel("Skies"));
    jTabbedPane.addTab("Green", new JLabel("Grass"));

    for (int i = 0; i < jTabbedPane.getTabCount(); i++) {
        final JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i));

        tabComponent.addMouseMotionListener(new MouseMotionAdapter() {
            @Override/*from w  w  w  .  j av a2s .  com*/
            public void mouseDragged(MouseEvent e) {
                System.out.println("tabComponent dragging");
            }
        });

        tabComponent.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int x = tabComponent.getLocationOnScreen().x - jTabbedPane.getLocationOnScreen().x;
                int y = tabComponent.getLocationOnScreen().y - jTabbedPane.getLocationOnScreen().y;
                MouseEvent me = new MouseEvent((JLabel) e.getSource(), e.getID(), e.getWhen(), e.getModifiers(),
                        x, y, e.getLocationOnScreen().x, e.getLocationOnScreen().y, e.getClickCount(),
                        e.isPopupTrigger(), e.getButton());
                jTabbedPane.getMouseListeners()[0].mousePressed(me);
                System.out.println("tabComponent mousePressed e=" + e);
            }
        });
        jTabbedPane.setTabComponentAt(i, tabComponent);
    }
    JFrame jFrame = new JFrame();
    jFrame.add(jTabbedPane);
    jFrame.setSize(400, 500);
    jFrame.setVisible(true);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

/**
 * Returns whether event involves middle mouse button or not.
 *
 * @param e mouse event//from   w w  w  .  j  a va2 s  .  c  om
 * @return true if event involves middle mouse button, false otherwise
 */
public static boolean isMiddleMouseButton(final MouseEvent e) {
    return (e.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK;
}

From source file:Main.java

/**
 * Returns whether event involves left mouse button or not.
 *
 * @param e/* w w w.  j  a va 2  s .c  o  m*/
 *            mouse event
 * @return true if event involves left mouse button, false otherwise
 */
public static boolean isLeftMouseButton(final MouseEvent e) {
    return (e.getModifiers() & InputEvent.BUTTON1_MASK) != 0;
}

From source file:Main.java

/**
 * Returns whether event involves right mouse button or not.
 *
 * @param e mouse event//  ww  w .j  ava2s. c  o  m
 * @return true if event involves right mouse button, false otherwise
 */
public static boolean isRightMouseButton(final MouseEvent e) {
    return (e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK;
}

From source file:Main.java

public static MouseEvent newMouseEvent(Component comp, int id, MouseEvent e) {
    return new MouseEvent(comp, id, e.getWhen(), e.getModifiers() | e.getModifiersEx(), e.getX(), e.getY(),
            e.getClickCount(), e.isPopupTrigger());
}

From source file:Main.java

static boolean isLeftButtonAndNoModifiers(final MouseEvent event) {
    final boolean leftMouseButton = SwingUtilities.isLeftMouseButton(event);
    if (leftMouseButton) {
        final int modifiers = event.getModifiers();
        if (InputEvent.BUTTON1_MASK == modifiers) {
            return true;
        }//from w ww  . j  av  a  2s. c  o  m
    }
    return false;
}