I l@ve RuBoard |
![]() ![]() |
7.8 SpinnersYou might be wondering just what a spinner is. It's a new 1.4 component similar to the JComboBox, but it shows only one item. It includes up and down arrows to "scroll" through its set of values. A JFormattedTextField is used to edit and render those values. Spinners are quite flexible. They work nicely with a set of choices (such as the months of the year) as well as with unbounded ranges such as a set of integers. Figure 7-13 shows several examples of spinners in different L&Fs. The Mac L&F is missing from this figure because the SDK 1.4 was not available on OS X at the time we went to press. Figure 7-13. Various JSpinner instances in three L&Fs![]() The classes involved in spinners are shown in Figure 7-14. Figure 7-14. JSpinner class diagram![]() 7.8.1 PropertiesJSpinner has several properties related to the values it displays (see Table 7-16). Most of the properties are easy to understand from their names alone. The currently selected value is available through the read/write value property.
7.8.2 Events
7.8.3 Constructors
7.8.4 Editing MethodsThe following methods may be of use to developers:
7.8.5 Simple SpinnersThe code for the spinner examples in Figure 7-13 is shown below. Notice that we use various model constructors to build the different spinners. The spinner models are discussed in the next section. // SpinnerTest.java // import javax.swing.*; import javax.swing.event.*; import java.awt.*; public class SpinnerTest extends JFrame { public SpinnerTest( ) { super("JSpinner Test"); setSize(300,180); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane( ); c.setLayout(new GridLayout(0,2)); c.add(new JLabel(" Basic Spinner")); c.add(new JSpinner( )); c.add(new JLabel(" Date Spinner")); c.add(new JSpinner(new SpinnerDateModel( ))); String weekdays[] = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; c.add(new JLabel(" List Spinner")); c.add(new JSpinner(new SpinnerListModel(weekdays))); c.add(new JLabel(" Number Spinner")); c.add(new JSpinner(new SpinnerNumberModel(0, 0, 100, 5))); c.add(new JLabel(" Rollover List Spinner")); c.add(new JSpinner(new RolloverSpinnerListModel(weekdays))); setVisible(true); } public static void main(String args[]) { new SpinnerTest( ); } } |
I l@ve RuBoard |
![]() ![]() |