devxlogo

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;}

size=3>

devx-admin

Share the Post: