Example usage for javax.swing SwingUtilities isRightMouseButton

List of usage examples for javax.swing SwingUtilities isRightMouseButton

Introduction

In this page you can find the example usage for javax.swing SwingUtilities isRightMouseButton.

Prototype

public static boolean isRightMouseButton(MouseEvent anEvent) 

Source Link

Document

Returns true if the mouse event specifies the right mouse button.

Usage

From source file:Main.java

public static void main(String[] arg) {
    DefaultListModel<String> listModel = new DefaultListModel<String>();
    for (int i = 0; i < 10; i++) {
        listModel.addElement("Item " + (i + 1));
    }//  w w w  .j  ava2 s.  c om

    JList<String> list = new JList<String>(listModel);
    list.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent me) {
            if (SwingUtilities.isRightMouseButton(me)) {
                list.clearSelection();
            }
        }
    });

    JScrollPane listScrollPane = new JScrollPane(list);
    JFrame f = new JFrame();
    f.getContentPane().add(listScrollPane);

    f.setSize(500, 400);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

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.");
            }//from w  ww . j a  v a  2 s  .  c o m
            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 ww w  .  java 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.");
        }/*  ww 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

/** If you use command-click (on a one-button mouse) to simulate right-click,
java.swing.SwingUtilities.isLeftMouseButton returns true (and so does
java.swing.SwingUtilities.isRightMouseButton), which results in the
wrong behavior. *//*from w  ww  .j  ava  2 s  .c o  m*/
public static boolean isLeftMouseButton(MouseEvent e) {
    if (SwingUtilities.isRightMouseButton(e))
        return false;
    else if (SwingUtilities.isLeftMouseButton(e))
        return true;
    else
        return false;
}

From source file:Main.java

/**
 * Returns whether the specifid mouse events triggers popup menu or not.
 * This method might act differently on different operating systems.
 *
 * @param e mouse event// ww  w.ja  v  a2s.c  o m
 * @return true if the specifid mouse events triggers popup menu, false otherwise
 */
public static boolean isPopupTrigger(final MouseEvent e) {
    return e.isPopupTrigger() || SwingUtilities.isRightMouseButton(e);
}

From source file:MouseClickListener.java

public void mouseClicked(MouseEvent e) {
    if (SwingUtilities.isLeftMouseButton(e)) {
        System.out.print("The mouse left button was clicked");
    } else if (SwingUtilities.isRightMouseButton(e)) {
        System.out.print("The mouse right button was clicked");
    } else if (SwingUtilities.isMiddleMouseButton(e)) {
        System.out.print("The mouse middle button was clicked");
    }/*from   ww  w. j av  a 2s. c  o m*/
    System.out.print(e.getClickCount() + "time(s)");
    int x = e.getX();
    int y = e.getY();
    System.out.println(" at (" + x + "," + y + ")");
}

From source file:de.weltraumschaf.minesweeper.control.FieldBoxButtonListener.java

@Override
public void mouseClicked(final MouseEvent e) {
    if (!(e.getComponent() instanceof FieldBoxButton)) {
        LOG.warn("Got click event from something else than FieldBoxButton!");
        return;//from   w w w  . j a v a  2s .c  o  m
    }

    final FieldBoxButton originatingButton = (FieldBoxButton) e.getComponent();

    if (originatingButton.isOpen()) {
        LOG.debug(String.format("Button %s already opened, exit.", originatingButton));
        return;
    }

    if (SwingUtilities.isRightMouseButton(e)) {
        LOG.debug(String.format("Right click on %s.", originatingButton.getBox()));

        if (originatingButton.isFlag()) {
            originatingButton.close();
        } else {
            originatingButton.flag();
        }
    } else {
        LOG.debug(String.format("Left click on %s", originatingButton.getBox()));

        if (originatingButton.isFlag()) {
            LOG.debug(String.format("Button %s flagged, exit.", originatingButton));
            return;
        }

        originatingButton.open();
    }

    originatingButton.getParent().repaint();
    main.getStatusbar().setMinesLeft(originatingButton.getBox().getField().countUnflaggedMines());
    checkForWon(originatingButton.getBox().getField());
}

From source file:jmupen.MyListSelectionListener.java

@Override
public void mousePressed(MouseEvent e) {
    list.setSelectedIndex(list.locationToIndex(e.getPoint()));
    int index = list.getSelectedIndex();
    if (SwingUtilities.isRightMouseButton(e)) {
        JPopupMenu menu = new JPopupMenu();
        JMenuItem item = new JMenuItem("Remove");
        item.addActionListener(new ActionListener() {
            @Override//from   w  w w  .j a  v a2 s .c  o m
            public void actionPerformed(ActionEvent e) {
                if (index != -1) {
                    try {
                        System.out.println("Linea: " + index + " " + model.get(index));
                        model.removeElementAt(index);
                        removeLines(index, JMupenUtils.getRecents().toFile());
                        JMupenUtils.setGames(JMupenUtils.getGamesFromFile(JMupenUtils.getRecents()));
                    } catch (IOException ex) {
                        System.err.println("Error removing recent game. " + ex.getLocalizedMessage());
                    }
                }
            }
        });
        menu.add(item);
        menu.show(list, e.getX(), e.getY());
    }
}

From source file:com.sec.ose.osi.ui.frm.tray.JTrayIconApp.java

private void initTray() {
    Image image = new ImageIcon(WindowUtil.class.getResource("icon.png")).getImage();

    mTrayIcon = new TrayIcon(image, mTrayTitle, createPopupMenu(BEFORE_LOGIN_STATE));
    mTrayIcon.setImageAutoSize(true);//from www  .j a va  2 s .  c om
    mTrayIcon.addMouseListener(new java.awt.event.MouseAdapter() {

        public void mousePressed(java.awt.event.MouseEvent e) {
            log.debug("mousePressed()");
            int state = 0;
            currentFrame = UISharedData.getInstance().getCurrentFrame();

            if (currentFrame == null)
                return;
            if (currentFrame.getClass().equals(JFrmLogin.class)) {
                state = BEFORE_LOGIN_STATE;
            } else {
                state = AFTER_LOGIN_STATE;
            }

            if (SwingUtilities.isRightMouseButton(e)) {
                mTrayIcon.setPopupMenu(createPopupMenu(state));
            }

            if (e.getClickCount() >= DOUBLE_CLICK && !e.isConsumed()) {
                currentFrame.setVisible(true);
            }
        }
    });

    try {
        mSystemTray.add(mTrayIcon);
    } catch (AWTException e1) {
        log.warn(e1.getMessage());
    }
}