19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

«interface»<br />

javax.swing.SpinnerModel<br />

javax.swing.AbstractSpinnerModel<br />

javax.swing.SpinnerListModel<br />

-list: java.util.List<br />

+SpinnerListModel()<br />

+SpinnerListModel(values: List)<br />

+SpinnerListModel(values: Object[])<br />

<strong>Java</strong>Beans properties with get and set<br />

methods omitted in the UML diagram<br />

S<strong>to</strong>res data in a list.<br />

Constructs a SpinnerListModel that contains “empty” string elements.<br />

Constructs a SpinnerListModel with the specified list.<br />

Constructs a SpinnerListModel with the specified array.<br />

Figure 39.11<br />

SpinnerListModel uses a java.util.List <strong>to</strong> s<strong>to</strong>re a sequence of data in<br />

the model.<br />

You can create a SpinnerListModel using an array or a list. For<br />

example, the following code creates a model that consists of<br />

values Freshman, Sophomore, Junior, Senior, and Graduate in an<br />

array.<br />

// Create an array<br />

String[] grades = {"Freshman", "Sophomore", "Junior",<br />

"Senior", "Graduate"};<br />

// Create a model from an array<br />

model = new SpinnerListModel(grades);<br />

Alternatively, the following code creates a model using a list:<br />

// Create an array<br />

String[] grades = {"Freshman", "Sophomore", "Junior",<br />

"Senior", "Graduate"};<br />

// Create an ArrayList from the array<br />

list = new ArrayList(Arrays.asList(grades));<br />

// Create a model from the list<br />

model = new SpinnerListModel(list);<br />

The alternative code seems unnecessary. However, it is useful if<br />

you need <strong>to</strong> add or remove elements from the model. The size of<br />

the array is fixed once the array is created. The list is a<br />

flexible data structure that enables you <strong>to</strong> add or remove<br />

elements dynamically.<br />

39.6.2 SpinnerNumberModel<br />

SpinnerNumberModel (see Figure 39.12) is a concrete<br />

implementation of SpinnerModel that represents a sequence of<br />

numbers. It contains the properties maximum, minimum, and<br />

stepSize. The maximum and minimum properties specify the upper<br />

and lower bounds of the sequence. The stepSize specifies the size<br />

of the increase or decrease <strong>com</strong>puted by the nextValue and<br />

previousValue methods defined in SpinnerModel. The minimum and<br />

15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!