Managing extra mouse buttons and high resolution mouse wheels : Swing « JDK 7 « Java

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;

public class Test {

  public static void main(String[] args) {
    ApplicationWindow window = new ApplicationWindow();

class ApplicationWindow extends JFrame implements MouseListener,
    MouseWheelListener {

  public ApplicationWindow() {
    this.setSize(200, 100);
    this.setLayout(new FlowLayout());
    JButton exitButton = new JButton("Exit");

    int totalButtons = MouseInfo.getNumberOfButtons();
    System.out.println("You have " + totalButtons + " total buttons");


    exitButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {


  public void mousePressed(MouseEvent e) {
    System.out.println("" + e.getButton());


  public void mouseWheelMoved(MouseWheelEvent e) {
    System.out.println("" + e.getPreciseWheelRotation() + " - "
        + e.getWheelRotation());

  public void mouseClicked(MouseEvent e) {

  public void mouseReleased(MouseEvent e) {

  public void mouseEntered(MouseEvent e) {

  public void mouseExited(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");



Related examples in the same category

1.Creating a varying gradient translucent window
2.Handling multiple file selection in the FileDialog class
3.Managing the Opacity of a Window
4.Managing the Shape of a Window
5.Managing Window types
6.Mixing heavyweight and lightweight components
7.New border types in Java 7:RaisedSoftBevelBorder
8.New border types in Java 7:LineBorder width
9.New border types in Java 7:LoweredSoftBevelBorder
11.BorderFactory.createSoftBevelBorder(BevelBorder.LOWERED, Color.lightGray, Color.yellow)
12.Using the new JLayer Decorator for a password field
13.Using the NumericShaper.Range enumeration to support the display of digits