Java Swing Tutorial - Java MouseEvent.getModifiersEx()








Syntax

MouseEvent.getModifiersEx() has the following syntax.

public int getModifiersEx()

Example

In the following code shows how to use MouseEvent.getModifiersEx() method.

/*from   w w w  . j a  v a 2s  .  co  m*/
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {
  JButton button = new JButton("click frame not me");

  public Main() {
    setLayout(null);
    add(button);
    button.setSize(button.getPreferredSize());
    button.setLocation(20, 20);
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent event) {
        int onmask = MouseEvent.SHIFT_DOWN_MASK | MouseEvent.BUTTON1_DOWN_MASK;    
        int offmask = MouseEvent.CTRL_DOWN_MASK;    
        if ((event.getModifiersEx() & (onmask | offmask)) == onmask) {
          System.out.println(event.getLocationOnScreen());
        } 
      }
    });
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("MoveButton");
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setSize(250, 200);
    frame.setLocation(200, 200);
    frame.setContentPane(new Main());
    frame.setVisible(true);
  }
}