Example usage for javax.swing SwingUtilities isLeftMouseButton

List of usage examples for javax.swing SwingUtilities isLeftMouseButton

Introduction

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

Prototype

public static boolean isLeftMouseButton(MouseEvent anEvent) 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) {
    final JTextPane tp = new JTextPane();
    JButton withFocus = new JButton("Select with focus");
    tp.addMouseListener(new MouseAdapter() {

        @Override//from   w ww  . ja v a  2 s. co  m
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
                tp.selectAll();
            }
        }

    });

    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(tp));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.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.");
            }/*  w w  w  . j ava2 s  . co 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 . j a  va2  s.  c  om*/
    };

    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.");
        }/*from  ww w  . ja  v a  2  s  .co 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

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;
        }//  www  . ja  va  2  s  . com
    }
    return false;
}

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  w  w .jav a  2s  .  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: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 w w w.j  a  v  a2 s.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:Main.java

/**
 * Ignore mouse events if the component is null, not enabled, the event
 * is not associated with the left mouse button, or the event has been
 * consumed.//from  w w w.ja  v a2s . c  o m
 */
public static boolean shouldIgnore(MouseEvent me, JComponent c) {
    return c == null || !c.isEnabled() || !SwingUtilities.isLeftMouseButton(me) || me.isConsumed();
}

From source file:Main.java

public Main() {
    setSize(500, 500);//from w  ww .j  a va  2 s  .  c om
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLayout(new GridLayout(0, 1));
    JPanel buttonPanel = new JPanel();

    JButton addButton = new JButton("add");
    addButton.addActionListener(e -> addElementToArrayList());
    JButton removeButton = new JButton("remove");
    removeButton.addActionListener(e -> removeElementFromArrayList());

    tabbedPane = new JTabbedPane();
    tabbedPaneMouseListener = (new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                if (e.getClickCount() == 1) {
                    System.out.println("Do Something");
                }
            }
        }
    });
    tabbedPane.addMouseListener(tabbedPaneMouseListener);

    buttonPanel.add(addButton);
    buttonPanel.add(removeButton);
    add(buttonPanel);
    this.add(tabbedPane);
}

From source file:inflor.core.gates.ui.PolygonGateAdapter.java

@Override
public void mouseClicked(MouseEvent e) {
    Point2D v = ChartUtils.getPlotCoordinates(e, panel);
    if (SwingUtilities.isLeftMouseButton(e)) {
        // add the next segment
        anchorPoint = v;//from  w ww  .  ja v a 2  s  . c om
        vertices.add(v);
        updateTemporaryAnnotation();
    }
    if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
        XYLineAnnotation closingSegment = new XYLineAnnotation(anchorPoint.getX(), anchorPoint.getY(),
                vertices.get(0).getX(), vertices.get(0).getY());
        segments.add(closingSegment);
        panel.addTemporaryAnnotation(closingSegment);

        // Finish the polygon and ask for a name
        int pointCount = vertices.size() * 2;
        double[] polygon = new double[pointCount];
        for (int i = 0; i < pointCount; i++) {
            polygon[i] = vertices.get(i / 2).getX();
            polygon[i + 1] = vertices.get(i / 2).getY();
            i++;//Sonar warning but wont fix, I think.
        }
        // Pop a gate editor dialog
        GateNameEditor dialog = new GateNameEditor();
        dialog.setVisible(true);
        // On Close...
        if (dialog.isOK()) {
            PolygonGateAnnotation finalPolygon = new PolygonGateAnnotation(dialog.getGateName(),
                    panel.getDomainAxisName(), panel.getRangeAxisName(), polygon, LookAndFeel.DEFAULT_STROKE,
                    LookAndFeel.DEFAULT_GATE_COLOR);
            panel.createGateAnnotation(finalPolygon);
        }
        dialog.dispose();

        // remove the anchor point && cleanup segments.
        vertices.clear();
        panel.removeTemporaryAnnotation(anchorSegment);
        anchorSegment = null;
        anchorPoint = null;
        segments.forEach(panel::removeTemporaryAnnotation);//TODO:if this is broken, maybe sonar was wrong?
        segments = null;
        panel.activateGateSelectButton();
    }
}