📄 spinner.html
字号:
public CyclingSpinnerListModel(Object[] values) { super(values); firstValue = values[0]; lastValue = values[values.length - 1]; } public void setLinkedModel(SpinnerModel linkedModel) { this.linkedModel = linkedModel; } public Object getNextValue() { Object value = super.getNextValue(); if (value == null) { value = firstValue; if (linkedModel != null) { linkedModel.setValue(linkedModel.getNextValue()); } } return value; } public Object getPreviousValue() { Object value = super.getPreviousValue(); if (value == null) { value = lastValue; if (linkedModel != null) { linkedModel.setValue(linkedModel.getPreviousValue()); } } return value; }}</pre></blockquote><p>The <code>CyclingSpinnerListModel</code> modelis used for the Month spinner in SpinnerDemo2,an example that is almost identical to SpinnerDemo.You can<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/SpinnerDemo2.jnlp">run SpinnerDemo2</a> (it requires release 6)using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>.To see its full source code, consult the<a href="examples/index.html#SpinnerDemo2">example index</a>.<p>As we mentioned before, if you implement a spinner modelthat does not descend from<code>SpinnerListModel</code>,<code>SpinnerNumberModel</code>, or<code>SpinnerDateModel</code>,then the spinner's default editor isan uneditable instance of <code>JSpinner.DefaultEditor</code>.As you've already seen,you can set the editor of a spinnerby invoking the <code>setEditor</code> method on the spinnerafter the spinner's model property has been set.An alternative to using <code>setEditor</code> is tocreate a subclass of <code>JSpinner</code>and override its <code>createEditor</code> methodso that it returns a particular kind of editorwhenever the spinner model is of a certain type.<p>In theory at least, you can use any <code>JComponent</code>as an editor.Possibilities include using asubclass of a standard componentsuch as <code>JLabel</code>,or a component you've implemented from scratch,or a subclass of<code>JSpinner.DefaultEditor</code>.The only requirementsare that the editor mustbe updated to reflect changes in the spinner's value,and it must have a reasonable preferred size.The editor should generally alsoset its tool tip textto whatever tool tip text has been specifiedfor the spinner.An example of implementing an editor is in the next section.</blockquote><h3><a name="change">Detecting Spinner Value Changes</a></h3><blockquote>You can detect that a spinner's value has changedby registering a change listener oneither the spinner or its model.Here's an example of implementing such a change listener.It's from SpinnerDemo3,which is based on SpinnerDemoand uses a change listenerto change the color of some textto match the value of the Another Date spinner.You can try it out by<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/SpinnerDemo3.jnlp">running SpinnerDemo3</a> (it requires release 6)using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>.For the full source code, see the<a href="examples/index.html#SpinnerDemo3">example index</a>.<blockquote><pre>public class SpinnerDemo3 extends JPanel implements ChangeListener { protected Calendar calendar; protected JSpinner dateSpinner; ... public SpinnerDemo3() { ... SpinnerDateModel dateModel = ...; ... setSeasonalColor(dateModel.getDate()); //initialize color //Listen for changes on the date spinner. dateSpinner.addChangeListener(this); ... } public void stateChanged(ChangeEvent e) { SpinnerModel dateModel = dateSpinner.getModel(); if (dateModel instanceof SpinnerDateModel) { setSeasonalColor(((SpinnerDateModel)dateModel).getDate()); } } protected void setSeasonalColor(Date date) { calendar.setTime(date); int month = calendar.get(Calendar.MONTH); JFormattedTextField ftf = getTextField(dateSpinner); if (ftf == null) return; //Set the color to match northern hemisphere seasonal conventions. switch (month) { case 2: //March case 3: //April case 4: //May ftf.setForeground(SPRING_COLOR); break; ... default: //December, January, February ftf.setForeground(WINTER_COLOR); } } ...}</pre></blockquote>The following example implements an editor,which has a change listenerso that it can reflect the spinner's current value.This particular editordisplays a solid color of gray,ranging anywhere from white to black.You can try it out by<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/SpinnerDemo4.jnlp">running SpinnerDemo4</a> (it requires release 6)using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>.For the full source code, see the<a href="examples/index.html#SpinnerDemo4">example index</a>.<blockquote><pre><em>...//Where the components are created:</em>JSpinner spinner = new JSpinner(new GrayModel(170));spinner.setEditor(new GrayEditor(spinner));class GrayModel extends SpinnerNumberModel { ...}class GrayEditor extends JLabel implements ChangeListener { public GrayEditor(JSpinner spinner) { setOpaque(true); ... //Get info from the model. GrayModel myModel = (GrayModel)(spinner.getModel()); setBackground(myModel.getColor()); spinner.addChangeListener(this); ... updateToolTipText(spinner); } protected void updateToolTipText(JSpinner spinner) { String toolTipText = spinner.getToolTipText(); if (toolTipText != null) { //JSpinner has tool tip text. Use it. if (!toolTipText.equals(getToolTipText())) { setToolTipText(toolTipText); } } else { //Define our own tool tip text. GrayModel myModel = (GrayModel)(spinner.getModel()); int rgb = myModel.getIntValue(); setToolTipText("(" + rgb + "," + rgb + "," + rgb + ")"); } } public void stateChanged(ChangeEvent e) { JSpinner mySpinner = (JSpinner)(e.getSource()); GrayModel myModel = (GrayModel)(mySpinner.getModel()); setBackground(myModel.getColor()); updateToolTipText(mySpinner); }}</pre></blockquote></blockquote><h3><a name="api">The Spinner API</a></h3><blockquote><p>The following tables list some of the commonly used APIfor using spinners.If you need to deal directly with the editor's formatted text field,you should also see<a href="formattedtextfield.html#api">The FormattedTextField API</a>.Other methods you might useare listed in the API tables in<a href="jcomponent.html#api">The JComponent Class</a>.<ul><li><a href="#newclassesapi">Classes Related to Spinners</a><li><a href="#jspinnerapi">Useful JSpinner Constructors and Methods</a><li><a href="#editorapi">Useful Editor Constructors and Methods</a><li><a href="#listapi">SpinnerListModel Methods</a><li><a href="#dateapi">SpinnerDateModel Methods</a><li><a href="#numberapi">SpinnerNumberModel Methods</a></ul><p><table border=1><caption><a name="newclassesapi">Classes Related to Spinners</a></caption><tr><th align=left>Class or Interface</th><th align=left>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JSpinner.html">JSpinner</a> </td> <td>A single-line input field that allows the user to select a number or object value from an ordered sequence. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpinnerModel.html">SpinnerModel</a> </td> <td>The interface implemented by all spinner models. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractSpinnerModel.html">AbstractSpinnerModel</a> </td> <td>The usual superclass for spinner model implementations. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpinnerListModel.html">SpinnerListModel</a> </td> <td>A subclass of <code>AbstractSpinnerModel</code> whose values are defined by an array or a <code>List</code>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpinnerDateModel.html">SpinnerDateModel</a> </td> <td>A subclass of <code>AbstractSpinnerModel</code> that supports sequences of <code>Date</code>s. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpinnerNumberModel.html">SpinnerNumberModel</a> </td> <td>A subclass of <code>AbstractSpinnerModel</code> that supports sequences of numbers. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JSpinner.DefaultEditor.html">JSpinner.DefaultEditor</a> </td> <td>Implements an uneditable component that displays the spinner's value. Subclasses of this class are generally more specialized (and editable). </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JSpinner.ListEditor.html">JSpinner.ListEditor</a> </td> <td>A subclass of <code>JSpinner.DefaultEditor</code> whose values are defined by an array or a <code>List</code>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JSpinner.DateEditor.html">JSpinner.DateEditor</a> </td> <td>A subclass of <code>JSpinner.DefaultEditor</code> that supports sequences of <code>Date</code>s. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JSpinner.NumberEditor.html">JSpinner.NumberEditor</a> </td> <td>A subclass of <code>JSpinner.DefaultEditor</code> that supports sequences of numbers. </td> </tr></table><p><table border=1><caption><a name="jspinnerapi">Useful JSpinner Constructors and Methods</a></caption><tr><th align=left>Constructor or Method
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -