Use Mouse Wheel to Control JSpinner

Use Mouse Wheel to Control JSpinner

Merlin (JDK1.4) introduces two new features: JSpinner and MouseWheelListener. Sometimes it is convenient to control the spinner values with the mouse wheel. Here’s an example:

 import javax.swing.*;import java.awt.event.*;public class TestSpinner extends JFrame implements MouseWheelListener{  public TestSpinner()  {    setSize( 100, 50 );    spinner = new JSpinner();    spinner.addMouseWheelListener( this );    getContentPane().add( spinner, "Center" );  }  public void mouseWheelMoved( MouseWheelEvent e )  {    spinner.setValue( new Integer( ((Integer)spinner.getValue()).intValue() - e.getWheelRotation() ) );  }  public static void main( String [] args )  {    TestSpinner t = new TestSpinner();    t.setVisible( true );  }  private JSpinner spinner;}

See also - an AI reverse image search platform

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist