A single line input field that lets the user select a
number or an object value from an ordered sequence. Spinners typically
provide a pair of tiny arrow buttons for stepping through the elements
of the sequence. The keyboard up/down arrow keys also cycle through the
elements. The user may also be allowed to type a (legal) value directly
into the spinner. Although combo boxes provide similar functionality,
spinners are sometimes preferred because they don't require a drop down list
that can obscure important data.
| Nested Class Summary: |
|---|
| public static class | JSpinner.DefaultEditor | A simple base class for more specialized editors
that displays a read-only view of the model's current
value with a JFormattedTextField. Subclasses
can configure the JFormattedTextField to create
an editor that's appropriate for the type of model they
support and they may want to override
the stateChanged and propertyChanged
methods, which keep the model and the text field in sync.
This class defines a dismiss method that removes the
editors ChangeListener from the JSpinner
that it's part of. The setEditor method knows about
DefaultEditor.dismiss, so if the developer
replaces an editor that's derived from JSpinner.DefaultEditor
its ChangeListener connection back to the
JSpinner will be removed. However after that,
it's up to the developer to manage their editor listeners.
Similarly, if a subclass overrides createEditor,
it's up to the subclasser to deal with their editor
subsequently being replaced (with setEditor).
We expect that in most cases, and in editor installed
with setEditor or created by a createEditor
override, will not be replaced anyway.
This class is the LayoutManager for it's single
JFormattedTextField child. By default the
child is just centered with the parents insets. |
| public static class | JSpinner.DateEditor | An editor for a JSpinner whose model is a
SpinnerDateModel. The value of the editor is
displayed with a JFormattedTextField whose format
is defined by a DateFormatter instance whose
minimum and maximum properties
are mapped to the SpinnerDateModel. |
| public static class | JSpinner.NumberEditor | An editor for a JSpinner whose model is a
SpinnerNumberModel. The value of the editor is
displayed with a JFormattedTextField whose format
is defined by a NumberFormatter instance whose
minimum and maximum properties
are mapped to the SpinnerNumberModel. |
| public static class | JSpinner.ListEditor | An editor for a JSpinner whose model is a
SpinnerListModel. |
| protected class | JSpinner.AccessibleJSpinner | AccessibleJSpinner implements accessibility
support for the JSpinner class. |
| Methods from javax.swing.JComponent: |
|---|
|
_paintImmediately, addAncestorListener, addNotify, addVetoableChangeListener, alwaysOnTop, checkIfChildObscuredBySibling, clientPropertyChanged, compWriteObjectNotify, componentInputMapChanged, computeVisibleRect, computeVisibleRect, contains, createToolTip, disable, dndDone, dropLocationForPoint, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getCreatedDoubleBuffer, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getGraphicsInvoked, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getManagingFocusBackwardTraversalKeys, getManagingFocusForwardTraversalKeys, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getWriteObjCounter, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPainting, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintForceDoubleBuffered, paintImmediately, paintImmediately, paintToOffscreen, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyBindings, processKeyBindingsForAllComponents, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, rectangleIsObscured, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, safelyGetGraphics, safelyGetGraphics, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setCreatedDoubleBuffer, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setDropLocation, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPaintingChild, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setUIProperty, setVerifyInputWhenFocusTarget, setVisible, setWriteObjCounter, shouldDebugGraphics, superProcessMouseMotionEvent, unregisterKeyboardAction, update, updateUI |
| Methods from java.awt.Component: |
|---|
|
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, dispatchEventImpl, doLayout, enable, enable, enableEvents, enableInputMethods, eventTypeEnabled, findNextFocusComponent, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, generateName, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getGraphicsConfigurationImpl, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setPeer, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, translateEvent, update, validate |
| Method from javax.swing.JSpinner Detail: |
public void addChangeListener(ChangeListener listener) {
if (modelListener == null) {
modelListener = new ModelListener();
getModel().addChangeListener(modelListener);
}
listenerList.add(ChangeListener.class, listener);
}
Adds a listener to the list that is notified each time a change
to the model occurs. The source of ChangeEvents
delivered to ChangeListeners will be this
JSpinner. Note also that replacing the model
will not affect listeners added directly to JSpinner.
Applications can add listeners to the model directly. In that
case is that the source of the event would be the
SpinnerModel. |
public void commitEdit() throws ParseException {
JComponent editor = getEditor();
if (editor instanceof DefaultEditor) {
((DefaultEditor)editor).commitEdit();
}
}
Commits the currently edited value to the SpinnerModel.
If the editor is an instance of DefaultEditor, the
call if forwarded to the editor, otherwise this does nothing. |
protected JComponent createEditor(SpinnerModel model) {
if (model instanceof SpinnerDateModel) {
return new DateEditor(this);
}
else if (model instanceof SpinnerListModel) {
return new ListEditor(this);
}
else if (model instanceof SpinnerNumberModel) {
return new NumberEditor(this);
}
else {
return new DefaultEditor(this);
}
}
This method is called by the constructors to create the
JComponent
that displays the current value of the sequence. The editor may
also allow the user to enter an element of the sequence directly.
An editor must listen for ChangeEvents on the
model and keep the value it displays
in sync with the value of the model.
Subclasses may override this method to add support for new
SpinnerModel classes. Alternatively one can just
replace the editor created here with the setEditor
method. The default mapping from model type to editor is:
-
SpinnerNumberModel => JSpinner.NumberEditor
-
SpinnerDateModel => JSpinner.DateEditor
-
SpinnerListModel => JSpinner.ListEditor
- all others =>
JSpinner.DefaultEditor
|
protected void fireStateChanged() {
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ChangeListener.class) {
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
}
}
}
Sends a ChangeEvent, whose source is this
JSpinner, to each ChangeListener.
When a ChangeListener has been added
to the spinner, this method method is called each time
a ChangeEvent is received from the model. |
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJSpinner();
}
return accessibleContext;
}
Gets the AccessibleContext for the JSpinner |
public ChangeListener[] getChangeListeners() {
return (ChangeListener[])listenerList.getListeners(
ChangeListener.class);
}
Returns an array of all the ChangeListeners added
to this JSpinner with addChangeListener(). |
public JComponent getEditor() {
return editor;
}
Returns the component that displays and potentially
changes the model's value. |
public SpinnerModel getModel() {
return model;
}
Returns the SpinnerModel that defines
this spinners sequence of values. |
public Object getNextValue() {
return getModel().getNextValue();
}
getModel().getNextValue()
|
public Object getPreviousValue() {
return getModel().getPreviousValue();
}
getModel().getPreviousValue()
|
public SpinnerUI getUI() {
return (SpinnerUI)ui;
}
Returns the look and feel (L&F) object that renders this component. |
public String getUIClassID() {
return uiClassID;
}
Returns the suffix used to construct the name of the look and feel
(L&F) class used to render this component. |
public Object getValue() {
return getModel().getValue();
}
getModel().getValue()
|
public void removeChangeListener(ChangeListener listener) {
listenerList.remove(ChangeListener.class, listener);
}
Removes a ChangeListener from this spinner. |
public void setEditor(JComponent editor) {
if (editor == null) {
throw new IllegalArgumentException("null editor");
}
if (!editor.equals(this.editor)) {
JComponent oldEditor = this.editor;
this.editor = editor;
if (oldEditor instanceof DefaultEditor) {
((DefaultEditor)oldEditor).dismiss(this);
}
editorExplicitlySet = true;
firePropertyChange("editor", oldEditor, editor);
revalidate();
repaint();
}
}
Changes the JComponent that displays the current value
of the SpinnerModel. It is the responsibility of this
method to disconnect the old editor from the model and to
connect the new editor. This may mean removing the
old editors ChangeListener from the model or the
spinner itself and adding one for the new editor. |
public void setModel(SpinnerModel model) {
if (model == null) {
throw new IllegalArgumentException("null model");
}
if (!model.equals(this.model)) {
SpinnerModel oldModel = this.model;
this.model = model;
if (modelListener != null) {
oldModel.removeChangeListener(modelListener);
this.model.addChangeListener(modelListener);
}
firePropertyChange("model", oldModel, model);
if (!editorExplicitlySet) {
setEditor(createEditor(model)); // sets editorExplicitlySet true
editorExplicitlySet = false;
}
repaint();
revalidate();
}
}
Changes the model that represents the value of this spinner.
If the editor property has not been explicitly set,
the editor property is (implicitly) set after the "model"
PropertyChangeEvent has been fired. The editor
property is set to the value returned by createEditor,
as in:
setEditor(createEditor(model));
|
public void setUI(SpinnerUI ui) {
super.setUI(ui);
}
Sets the look and feel (L&F) object that renders this component. |
public void setValue(Object value) {
getModel().setValue(value);
}
getModel().setValue(value)
|
public void updateUI() {
setUI((SpinnerUI)UIManager.getUI(this));
invalidate();
}
Resets the UI property with the value from the current look and feel. |