JS today took a look at the new Jspinner in JDK1.4, which makes it easy to select dates, numbers, or options in a list.
JSpinner sample
The user uses the move up and down arrows on the component or keyboard to select. They can also enter their own choice. However, unlike JComboBox, JSpinner does not provide Drop-down list selection, so each choice and their order should have a certain meaning.
To use a class, you can simply create a collection of elements for selection (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);
Depending on the type of input you use, there are several help classes that you can use to create the component Data Model:
Spinnerdatemodel: Used to accept date input. This class supports changing the date by setting the constants in the Calendar class to different values; For example, calendar.week_of_month changes the date one week at a time.
Spinnerlistmodel: Used to accept input from a list of values.
Spinnernumbermodel: The input that is used to accept a range of digits (int or double) that has a set step size.
Each Spinnermodel execution depends on the editor used to enter the value. This editor must be a jcomponent, system-defined editor subclass Jspinner.defaulteditor. One of these can be used for each model:
Jspinner.dateeditor: For Spinnerdatemodel. Allows you to customize the input date format.
Jspinner.listeditor: For Spinnerlistmodel. Supports type-ahead to position values.
Jspinner.numbereditor: For Spinnernumbermodel. Allows you to customize the decimal format pattern.
Event handling
The JSpinner component works like other Swing components. If you are interested in identifying the time that the user changed the selection, please connect to a listener. For JSpinner, a listener is a changelistener that you can directly connect to JSpinner or its spinnermodel. Although you can connect listeners to either of these, the ChangeEvent source is always spinnermodel when the value changes:
Listing 2. JSpinner Event Listening
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 an example of using all three different spinner models (listing 3). The list model uses a set of month names that are fetched from the DateFormatSymbols class. The date model example changes the input format of the editor. When you use the arrow next to this field, you can also move the date one week at a time. The digital model example lets the user select a number from 0 to 100, and jumps 5 numbers at a time when the arrow is used. Please note that users can enter any number, not just multiples of 5.
For all components, when each spinner value does change, the attached listener will show the same change. If you use the cursor keys to change the month, day, or year, you will notice that the values do not change until you press the ENTER key.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.