Today I use a new JSPinner in JDK1.4, which is convenient to select options in the date, number, or list.
Jspinner example
Users choose to select up and down arrows on the component or keyboard. They can also enter their choices. However, different from JCOMBOBOX, JSPinner does not provide drop-down list selection, so each selection and their order should have a certain meaning.
To use the class, you can simply create a set of elements (in a list or array), create a spinnermodel from the list, and create a JSPinner for the model:
Listing 1. Simple JSPinner Usage
String [] months = new dateformatsymbols (). GetMonths ();
Spinnermodel Model = New SpinnerListModel (MONTHS);
Jspinner Spinner = New Jspinner (Model);
Based on the type you use, there are the following help classes that can be used to create component data models:
SpinnerDateModel: It is used to accept the date input. This class supports changes the date by setting constants in the Calendar class to different values; for example, Calendar.Week_OF_MOTH changes the date for a week. SpinnerListModel: It is used to accept input from a value list. SpinnerNumbermodel: It is used to accept inputs within a certain range of numbers (INT or DOUBLE) have been set.
Each SpinnerModel executes an editor that is used for input values. This editor must be a JComponent; system-defined editor subclass jspinner.defaulteditor. One of them can be used for each model:
Jspinner.DateEditor: For SpinnerDateModel. Allows you to customize the input date format. JSPinner.Listeditor: For SpinnerListModel. Support Type-ahead to position values. Jspinner.Numbereditor: For SpinnerNumbermodel. Allows you to customize the decimal format.
Event Processing JSpinner components work like other Swing components. If you are interested in detecting a user changing the selected time, even a listener. For JSPinner, the listener is a ChangeListener, you can connect it directly to JSpinner or its spinnermodel. Although you can connect the listener to any of the two, when the value changes, the source of Changeevent is always spinnermodel:
Listing 2. Jspinner event listens
Changelistener Listener = New ChangeListener () {
Public Void StateChanged (ChangeEvent E) {
Spinnermodel Source = (spinnermodel) E.GETSOURCE ();
System.out.println ("The value is:" source.getValue ());
}
}
Model.Addchangelistener (Listener);
A complete example let's take a look at all three different SPNNER models (Listing 3). The list model uses the month name set from the DateFormatsyMbols class. Date Model Example Change the input format of the editor. When using the arrow next to this field, you can move the date once a week. The digital model example allows the user to select a number between 0 and 100, and 5 numbers each time when using an arrow. Note: The user can enter any number and is not limited to 5 times. For all components, the connected listeners will display the same change when each SPNNER value does change. If you use the cursor keys to change the moon, day or year, you will notice until the valid is pressed.
Listing 3. JSPINNER complete example
Import javax.swing. *;
Import javax.swing.event. *;
Import java.text. *;
Import java.awt. *;
Import java.util. *;
Public class spinner {
Public static void main (string args []) throws exception {
JFrame Frame = New Jframe ("spinner");
Frame.setDefaultCloseOperation (3);
String [] months = new dateformatsymbols (). GetMonths ();
Spinnermodel Model = New SpinnerListModel (MONTHS);
Jspinner Spinner = New Jspinner (Model);
Frame.getContentPane (). Add (spinner, borderlayout.north);
Spinnerdatemodel model2 = new spinnerdatemodel ();
Model2.setcalendarfield (Calendar.Week_OF_MONTH);
Jspinner Spinner2 = New Jspinner (Model2);
Jspinner.dateEditor Editor2 = New Jspinner.dateEditor
Spinner2, "MMMMM DD, YYYY");
Spinner2.seteditor (editor2);
Frame.getContentPane (). Add (spinner2, borderlayout.south);
SpinnerNumbermodel Model3 = New SpinnerNumbermodel (50, 0, 100, 5);
Jspinner Spinner3 = New Jspinner (Model3);
Frame.getContentPane (). Add (spinner3, borderlayout.center);
Changelistener Listener = New ChangeListener () {
Public Void StateChanged (ChangeEvent E) {
Spinnermodel Source = (spinnermodel) E.GETSOURCE ();
System.out.println ("The value is:" source.getValue ());
}
}
Model.Addchangelistener (Listener);
Model2.addchangelistener (Listener);
Model3.addchangelistener (Listener);
Frame.PACK ();
Frame.show ();
}
}
============================================================================================================================================================================================================= =============
Experience in use: Change the content outside the text basket or exception (such as between 0 ~ 100, I entered 101), jspinner.getvalue () gets the last preserved Correct value; such as entering normal numbers "15", enter, then empty this column, enter. Jspinner.getValue () can get 15 instead of returning NULL or an exception.