33.6. Storing and Retrieving Images in JDBC

 
[Page 1016 ( continued )]

30.5. JSpinner

A spinner is a text field with a pair of tiny arrow buttons on its right side that enable the user to select numbers , dates, or values from an ordered sequence, as shown in Figure 30.7. The keyboard up/down arrow keys also cycle through the elements. The user may also be allowed to type a (legal) value directly into the spinner. A spinner is similar to a combo box, but a spinner is sometimes preferred because it doesn't require a drop-down list that can obscure important data.


[Page 1017]
Figure 30.7. Two JSpinner components enable the user to select a month and a year for the calendar.


Figure 30.8 shows the constructors and commonly used methods in JSpinner . A JSpinner 's sequence value is defined by the SpinnerModel interface, which manages a potentially unbounded sequence of elements. The model doesn't support indexed random access to sequence elements. Only three sequence elements are accessible at a time, current, next , and previous, using the methods getValue() , getNextValue() , and getPreviousValue() , respectively. The current sequence element can be modified using the setValue method. When the current value in a spinner is changed, the model invokes the stateChanged(javax.swing.event.ChangeEvent e) method of the registered listeners. The listeners must implement javax.swing.event.ChangeListener . All these methods in SpinnerModel are also defined in JSpinner for convenience, so you can access the data in the model from JSpinner directly.

Figure 30.8. JSpinner uses a spinner model to store data.

Note

If you create a JSpinner object without specifying a model, the spinner displays a sequence of integers.


Listing 30.5 gives an example that creates a JSpinner object for a sequence of numbers and displays the previous, current, and next numbers from the spinner on a label, as shown in Figure 30.9.


[Page 1018]
Figure 30.9. The previous, current, and next values in the spinner are displayed on the label.


Listing 30.5. SimpleSpinner.java
 1   import   javax.swing.*;  2   import   javax.swing.event.*;  3   import   java.awt.BorderLayout;  4  5   public class   SimpleSpinner   extends   JApplet {  6  // Create a JSpinner  7    private   JSpinner spinner =   new   JSpinner();  8  9  // Create a JLabel  10   private   JLabel label =   new   JLabel(   " "   , JLabel.CENTER); 11 12   public   SimpleSpinner() { 13  // Add spinner and label to the UI  14     add(spinner, BorderLayout.NORTH); 15     add(label, BorderLayout.CENTER); 16 17  // Register and create a listener  18  spinner.addChangeListener(  new  ChangeListener()  { 19    public void   stateChanged(javax.swing.event.ChangeEvent e)  { 20         label.setText(   "Previous value: "   +  spinner.getPreviousValue()  21           +   " Current value: "   +  spinner.getValue()  22           +   " Next value: "   +  spinner.getNextValue()  ); 23       } 24     }); 25   } 26 } 

A JSpinner object is created using its no-arg constructor (line 7). By default, a spinner displays a sequence of integers.

An anonymous inner class event adapter is created to process the value change event on the spinner (lines 18 “24). The previous, current, and next values in a spinner can be obtained using the JSpinner 's instance methods getPreviousValue() , getValue() , and getNextValue() .

To display a sequence of values other than integers, you have to use spinner models.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net