Java examples for Swing:Mouse Event
Managing extra mouse buttons and high resolution mouse wheels
import java.awt.FlowLayout; import java.awt.MouseInfo; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Main extends JFrame implements MouseListener, MouseWheelListener{ public Main() { this.setTitle("Example"); this.setSize(200, 100); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new FlowLayout()); JButton exitButton = new JButton("Exit"); this.add(exitButton); int totalButtons = MouseInfo.getNumberOfButtons(); System.out.println(Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled()); System.out.println("You have " + totalButtons + " total buttons"); //from w w w . jav a 2 s . co m this.addMouseListener(this); this.addMouseWheelListener(this); exitButton.addActionListener(e->System.exit(0)); } public void mousePressed(MouseEvent e) { System.out.println("" + e.getButton()); } public void mouseWheelMoved(MouseWheelEvent e) { System.out.println("" + e.getPreciseWheelRotation() + " - " + e.getWheelRotation()); } @Override public void mouseClicked(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void mouseReleased(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void mouseEntered(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void mouseExited(MouseEvent e) { throw new UnsupportedOperationException("Not supported yet."); } public static void main(String[] args) { SwingUtilities.invokeLater(()->{ Main window = new Main(); window.setVisible(true); }); } }
There are two ways of controlling whether extra mouse buttons are enabled or not.
java -Dsun.awt.enableExtraMouseButtons=false ApplicationDriver System.setProperty("sun.awt.enableExtraMouseButtons", "true");