Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Sep 10, 2001

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;
}
Leslie Lu
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date