The code below would produce the following applet:
The following explains the code in the JSpinner life cycle.
First you need to set up a Spinner model. This holds the words that appear on the spinner.
Set up the names that will go in the spinner. This is an array of Strings. | String monthNames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
Put the monthNames, from above in the SpinnerModel. | SpinnerListModel months = new SpinnerListModel (monthNames); |
Then you can make the JSpinner and add in the Spinner model from above.
Declare the spinner (Globally so that you can access it below) | JSpinner spinner; |
Construct it, passing in the SpinnerListModel created above. (In init) | spinner = new JSpinner (months); |
Add in the spinner to the applet. (In init) | add (spinner); |
Get the value choosen. | spinner.getValue (); |
Get the value choosen and print it out. | showStatus ("" + spinner.getValue ()); |
Additional examples can be found on:
http://java.sun.com/docs/books/tutorial/uiswing/components/spinner.html
The complete code is as follows:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Spinning extends Applet implements ActionListener
{
JSpinner spinner;
public void init ()
{
String monthNames[] = {"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"};
SpinnerListModel months = new SpinnerListModel (monthNames);
spinner = new JSpinner (months);
add (spinner);
JButton done = new JButton ("Done");
done.setActionCommand ("Done");
done.addActionListener (this);
add (done);
}
public void actionPerformed (ActionEvent e)
{
showStatus ("" + spinner.getValue ());
}
}