One of the few standard Java Swing controls that does not have any Matlab uicontrol counterpart is
JSpinner is basically an editbox with two tiny adjacent up/down buttons. Spinners are similar in functionality to a combo-box (a.k.a. drop-down or pop-up menu), where a user can switch between several pre-selected values. They are often used when the list of possible values is too large to display in a combo-box menu. Like combo-boxes, spinners too can be editable (meaning that the user can type a value in the editbox) or not (the user can only “spin” the value using the up/down buttons).
JSpinner uses an internal data model, similarly to
JTable and other complex controls. The default model is
SpinnerNumberModel, which defines a min/max value (unlimited= by default) and step-size (1 by default). Additional predefined models are
SpinnerListModel (which accepts a cell array of possible string values) and
SpinnerDateModel (which defines a date range and step unit).
Here’s a basic code snippet showing how to display a simple numeric spinner for numbers between 20 and 35, with an initial value of 24 and increments of 1:
jModel = javax.swing.SpinnerNumberModel(24,20,35,1); jSpinner = javax.swing.JSpinner(jModel); jhSpinner = javacomponent(jSpinner, [10,10,60,20], gcf);
The spinner value can be set using the edit-box or by clicking on one of the tiny arrow buttons, or programmatically by setting the Value property. The spinner object also has related read-only properties NextValue and PreviousValue. The spinner’s model object has the corresponding Value (settable), NextValue (read-only) and PreviousValue (read-only) properties. In addition, the model also has the settable Maximum, Minimum and StepSize properties.
To attach a data-change callback, set the spinner’s StateChangedCallback property.
I have created a small Matlab demo, SpinnerDemo, which demonstrates usage of
JSpinner in Matlab figures. Each of the three predefined models (number, list, and date) is presented, and the spinner values are inter-connected via their callbacks. The Matlab code is modeled after the Java code that is used to document
JSpinner in the official documentation. Readers are welcome to download this demo from the Matlab File Exchange and reuse its source code.
As can be seen from the screenshot, SpinnerDemo also demonstrates how to attach a label to a GUI control with an associated accelerator key (Alt-D in the screenshot example, which sets the focus to the Date control).
An internal component in Matlab, namely
javax.swing.JSpinner, but in this particular case I cannot see any big advantage of using the internal
MJSpinnerrather than the standard
JSpinner. On the contrary, using
JSpinnerwill likely improve forward compatibility – MathWorks may well change
MJSpinnerin the future, but it cannot do anything to the standard Swing
JSpinner. In other cases, internal Matlab controls do offer significant advantages over the standard Swing controls, but not here it would seem. In any case, the SpinnerDemo utility uses
MJSpinner, but you can safely use
JSpinnerinstead (line #86).
The internal Matlab controls are discussed in detail in Chapter 5 of my Matlab-Java book, and
MJSpinneris specifically discussed in section 5.2.1.
JSpinnerderivative is JIDE’s
com.jidesoft.grid.SpinnerCellEditor, which can be used as the cell-editor component in tables. An example of this was shown in the article about Advanced JIDE Property Grids (and section 5.7.5 in the book). You may also be interested in the
com.jidesoft.combobox.DateSpinnerComboBox, which presents a control that includes both a date-selection combo-box and a spinner (section 5.7.2):