APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing.plaf.basic

public Class BasicSpinnerUI

extends SpinnerUI
Class Inheritance
Known Direct Subclasses
javax.swing.plaf.synth.SynthSpinnerUI
Imports
java.awt.AWTEvent, .Component, .ComponentOrientation, .Container, .Dimension, .FocusTraversalPolicy, .Font, .Insets, .KeyboardFocusManager, .LayoutManager, java.awt.event.ActionEvent, .FocusEvent, .FocusListener, .MouseEvent, .MouseListener, java.beans.PropertyChangeEvent, .PropertyChangeListener, java.text.AttributedCharacterIterator, .CharacterIterator, .DateFormat, .Format, .ParseException, java.util.Calendar, .Map, javax.swing.AbstractAction, .ButtonModel, .InputMap, .JButton, .JComponent, .JFormattedTextField, .JPanel, .JSpinner, .JTextField, .LookAndFeel, .SpinnerDateModel, .SpinnerModel, .SwingConstants, .SwingUtilities, .UIManager, javax.swing.border.Border, .CompoundBorder, javax.swing.event.ChangeEvent, .ChangeListener, javax.swing.plaf.ComponentUI, .FontUIResource, .SpinnerUI, .UIResource, javax.swing.text.InternationalFormatter, sun.swing.DefaultLookup

The default Spinner UI delegate.
Author
Hans Muller
Since
1.4

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
BasicSpinnerUI.ArrowButtonHandler

A handler for spinner arrow button mouse and action events.

private static class

Field Summary

Modifier and TypeField and Description
private BasicSpinnerUI.Handler
private static final BasicSpinnerUI.ArrowButtonHandler
nextButtonHandler

The mouse/action listeners that are added to the spinner's arrow buttons.

private static final BasicSpinnerUI.ArrowButtonHandler
private PropertyChangeListener
protected JSpinner
spinner

The spinner that we're a UI delegate for.

private static final Dimension
zeroSize

Used by the default LayoutManager class - SpinnerLayout for missing (null) editor/nextButton/previousButton children.

Constructor Summary

AccessConstructor and Description
public
BasicSpinnerUI()

Constructs a BasicSpinnerUI.

Method Summary

Modifier and TypeMethod and Description
private Component
createArrowButton(int direction)

protected JComponent

Returns:

the JSpinners editor JComponent, spinner.getEditor() by default
createEditor
()

This method is called by installUI to get the editor component of the JSpinner.

protected LayoutManager

Returns:

a LayoutManager for the editor, next button, and previous button.
createLayout
()

Creates a LayoutManager that manages the editor, nextButton, and previousButton children of the JSpinner.

protected Component

Returns:

a component that will replace the spinner's value with the next value in the sequence, or null
createNextButton
()

Creates an increment button, i.e. component that replaces the spinner value with the object returned by spinner.getNextValue.

protected Component

Returns:

a component that will replace the spinner's value with the previous value in the sequence, or null
createPreviousButton
()

Creates a decrement button, i.e. component that replaces the spinner value with the object returned by spinner.getPreviousValue.

protected PropertyChangeListener

Returns:

A PropertyChangeListener for the JSpinner itself
createPropertyChangeListener
()

Creates a PropertyChangeListener that can be added to the JSpinner itself.

public static ComponentUI

Returns:

a new BasicSpinnerUI object
createUI
(JComponent
the JSpinner (not used)
c
)

Hides javax.swing.plaf.ComponentUI.createUI.

Returns a new instance of BasicSpinnerUI.

public int
getBaseline(JComponent c, int width, int height)

Overrides javax.swing.plaf.ComponentUI.getBaseline.

Returns the baseline.

public Component.BaselineResizeBehavior
getBaselineResizeBehavior(JComponent c)

Overrides javax.swing.plaf.ComponentUI.getBaselineResizeBehavior.

Returns an enum indicating how the baseline of the component changes as the size changes.

private BasicSpinnerUI.Handler
private InputMap
getInputMap(int condition)

Returns the InputMap to install for condition.

private void
protected void
installDefaults()

Initialize the JSpinner border, foreground, and background, properties based on the corresponding "Spinner.*" properties from defaults table.

private void
installEditorBorderListener(JComponent editor)

Remove the border around the inner editor component for LaFs that install an outside border around the spinner,

protected void
installKeyboardActions()

Installs the keyboard Actions onto the JSpinner.

protected void
installListeners()

Initializes PropertyChangeListener with a shared object that delegates interesting PropertyChangeEvents to protected methods.

protected void
installNextButtonListeners(Component
Component to install the listeners on
c
)

Installs the necessary listeners on the next button, c, to update the JSpinner in response to a user gesture.

protected void
installPreviousButtonListeners(Component
Component to install the listeners on.
c
)

Installs the necessary listeners on the previous button, c, to update the JSpinner in response to a user gesture.

public void
installUI(JComponent
the JSpinner
c
)

Overrides javax.swing.plaf.ComponentUI.installUI.

Calls installDefaults, installListeners, and then adds the components returned by createNextButton, createPreviousButton, and createEditor.

pack-priv static void
private void
private void
maybeRemoveEditorBorder(JComponent editor)

Remove the border around the inner editor component for LaFs that install an outside border around the spinner,

private void
protected void
replaceEditor(JComponent
an old instance of editor
oldEditor
,
JComponent
a new instance of editor
newEditor
)

Called by the PropertyChangeListener when the JSpinner editor property changes.

protected void
uninstallDefaults()

Sets the JSpinner's layout manager to null.

protected void
uninstallListeners()

Removes the PropertyChangeListener added by installListeners.

public void
uninstallUI(JComponent
the JSpinner (not used)
c
)

Overrides javax.swing.plaf.ComponentUI.uninstallUI.

Calls uninstallDefaults, uninstallListeners, and then removes all of the spinners children.

private void
private void
updateEnabledState()

Updates the enabled state of the children Components based on the enabled state of the JSpinner.

private void
updateEnabledState(Container c, boolean enabled)

Recursively updates the enabled state of the child Components of c.

Field Detail

handlerback to summary
private BasicSpinnerUI.Handler handler
nextButtonHandlerback to summary
private static final BasicSpinnerUI.ArrowButtonHandler nextButtonHandler

The mouse/action listeners that are added to the spinner's arrow buttons. These listeners are shared by all spinner arrow buttons.

See Also
createNextButton, createPreviousButton
previousButtonHandlerback to summary
private static final BasicSpinnerUI.ArrowButtonHandler previousButtonHandler
propertyChangeListenerback to summary
private PropertyChangeListener propertyChangeListener
spinnerback to summary
protected JSpinner spinner

The spinner that we're a UI delegate for. Initialized by the installUI method, and reset to null by uninstallUI.

See Also
installUI, uninstallUI
zeroSizeback to summary
private static final Dimension zeroSize

Used by the default LayoutManager class - SpinnerLayout for missing (null) editor/nextButton/previousButton children.

Constructor Detail

BasicSpinnerUIback to summary
public BasicSpinnerUI()

Constructs a BasicSpinnerUI.

Method Detail

createArrowButtonback to summary
private Component createArrowButton(int direction)
createEditorback to summary
protected JComponent createEditor()

This method is called by installUI to get the editor component of the JSpinner. By default it just returns JSpinner.getEditor(). Subclasses can override createEditor to return a component that contains the spinner's editor or null, if they're going to handle adding the editor to the JSpinner in an installUI override.

Typically this method would be overridden to wrap the editor with a container with a custom border, since one can't assume that the editors border can be set directly.

The replaceEditor method is called when the spinners editor is changed with JSpinner.setEditor. If you've overridden this method, then you'll probably want to override replaceEditor as well.

Returns:JComponent

the JSpinners editor JComponent, spinner.getEditor() by default

See Also
installUI, replaceEditor, JSpinner#getEditor
createLayoutback to summary
protected LayoutManager createLayout()

Creates a LayoutManager that manages the editor, nextButton, and previousButton children of the JSpinner. These three children must be added with a constraint that identifies their role: "Editor", "Next", and "Previous". The default layout manager can handle the absence of any of these children.

Returns:LayoutManager

a LayoutManager for the editor, next button, and previous button.

See Also
createNextButton, createPreviousButton, createEditor
createNextButtonback to summary
protected Component createNextButton()

Creates an increment button, i.e. component that replaces the spinner value with the object returned by spinner.getNextValue. By default the nextButton is a JButton. If the increment button is not needed this method should return null.

Returns:Component

a component that will replace the spinner's value with the next value in the sequence, or null

See Also
installUI, createPreviousButton, installNextButtonListeners
createPreviousButtonback to summary
protected Component createPreviousButton()

Creates a decrement button, i.e. component that replaces the spinner value with the object returned by spinner.getPreviousValue. By default the previousButton is a JButton. If the decrement button is not needed this method should return null.

Returns:Component

a component that will replace the spinner's value with the previous value in the sequence, or null

See Also
installUI, createNextButton, installPreviousButtonListeners
createPropertyChangeListenerback to summary
protected PropertyChangeListener createPropertyChangeListener()

Creates a PropertyChangeListener that can be added to the JSpinner itself. Typically, this listener will call replaceEditor when the "editor" property changes, since it's the SpinnerUI's responsibility to add the editor to the JSpinner (and remove the old one). This method is called by installListeners.

Returns:PropertyChangeListener

A PropertyChangeListener for the JSpinner itself

See Also
installListeners
createUIback to summary
public static ComponentUI createUI(JComponent c)

Hides javax.swing.plaf.ComponentUI.createUI.

Returns a new instance of BasicSpinnerUI. SpinnerListUI delegates are allocated one per JSpinner.

Parameters
c:JComponent

the JSpinner (not used)

Returns:ComponentUI

a new BasicSpinnerUI object

See Also
ComponentUI#createUI
getBaselineback to summary
public int getBaseline(JComponent c, int width, int height)

Overrides javax.swing.plaf.ComponentUI.getBaseline.

Returns the baseline.

Exceptions
NullPointerException:
if c is null
IllegalArgumentException:
if width or height is < 0
Since
1.6
See Also
javax.swing.JComponent#getBaseline(int, int)
getBaselineResizeBehaviorback to summary
public Component.BaselineResizeBehavior getBaselineResizeBehavior(JComponent c)

Overrides javax.swing.plaf.ComponentUI.getBaselineResizeBehavior.

Returns an enum indicating how the baseline of the component changes as the size changes.

Exceptions
NullPointerException:
if c is null
Since
1.6
See Also
javax.swing.JComponent#getBaseline(int, int)
getHandlerback to summary
private BasicSpinnerUI.Handler getHandler()
getInputMapback to summary
private InputMap getInputMap(int condition)

Returns the InputMap to install for condition.

installButtonListenersback to summary
private void installButtonListeners(Component c, BasicSpinnerUI.ArrowButtonHandler handler)
installDefaultsback to summary
protected void installDefaults()

Initialize the JSpinner border, foreground, and background, properties based on the corresponding "Spinner.*" properties from defaults table. The JSpinners layout is set to the value returned by createLayout. This method is called by installUI.

See Also
uninstallDefaults, installUI, createLayout, LookAndFeel#installBorder, LookAndFeel#installColors
installEditorBorderListenerback to summary
private void installEditorBorderListener(JComponent editor)

Remove the border around the inner editor component for LaFs that install an outside border around the spinner,

installKeyboardActionsback to summary
protected void installKeyboardActions()

Installs the keyboard Actions onto the JSpinner.

Since
1.5
installListenersback to summary
protected void installListeners()

Initializes PropertyChangeListener with a shared object that delegates interesting PropertyChangeEvents to protected methods.

This method is called by installUI.

See Also
replaceEditor, uninstallListeners
installNextButtonListenersback to summary
protected void installNextButtonListeners(Component c)

Installs the necessary listeners on the next button, c, to update the JSpinner in response to a user gesture.

Parameters
c:Component

Component to install the listeners on

Exceptions
NullPointerException:
if c is null.
Since
1.5
See Also
createNextButton
installPreviousButtonListenersback to summary
protected void installPreviousButtonListeners(Component c)

Installs the necessary listeners on the previous button, c, to update the JSpinner in response to a user gesture.

Parameters
c:Component

Component to install the listeners on.

Exceptions
NullPointerException:
if c is null.
Since
1.5
See Also
createPreviousButton
installUIback to summary
public void installUI(JComponent c)

Overrides javax.swing.plaf.ComponentUI.installUI.

Calls installDefaults, installListeners, and then adds the components returned by createNextButton, createPreviousButton, and createEditor.

Parameters
c:JComponent

the JSpinner

See Also
installDefaults, installListeners, createNextButton, createPreviousButton, createEditor
loadActionMapback to summary
pack-priv static void loadActionMap(LazyActionMap map)
maybeAddback to summary
private void maybeAdd(Component c, String s)
maybeRemoveEditorBorderback to summary
private void maybeRemoveEditorBorder(JComponent editor)

Remove the border around the inner editor component for LaFs that install an outside border around the spinner,

removeEditorBorderListenerback to summary
private void removeEditorBorderListener(JComponent editor)
replaceEditorback to summary
protected void replaceEditor(JComponent oldEditor, JComponent newEditor)

Called by the PropertyChangeListener when the JSpinner editor property changes. It's the responsibility of this method to remove the old editor and add the new one. By default this operation is just:

spinner.remove(oldEditor);
spinner.add(newEditor, "Editor");
The implementation of replaceEditor should be coordinated with the createEditor method.
Parameters
oldEditor:JComponent

an old instance of editor

newEditor:JComponent

a new instance of editor

See Also
createEditor, createPropertyChangeListener
uninstallDefaultsback to summary
protected void uninstallDefaults()

Sets the JSpinner's layout manager to null. This method is called by uninstallUI.

See Also
installDefaults, uninstallUI
uninstallListenersback to summary
protected void uninstallListeners()

Removes the PropertyChangeListener added by installListeners.

This method is called by uninstallUI.

See Also
installListeners
uninstallUIback to summary
public void uninstallUI(JComponent c)

Overrides javax.swing.plaf.ComponentUI.uninstallUI.

Calls uninstallDefaults, uninstallListeners, and then removes all of the spinners children.

Parameters
c:JComponent

the JSpinner (not used)

updateEditorAlignmentback to summary
private void updateEditorAlignment(JComponent editor)
updateEnabledStateback to summary
private void updateEnabledState()

Updates the enabled state of the children Components based on the enabled state of the JSpinner.

updateEnabledStateback to summary
private void updateEnabledState(Container c, boolean enabled)

Recursively updates the enabled state of the child Components of c.

javax.swing.plaf.basic back to summary

private Class BasicSpinnerUI.ArrowButtonHandler

extends AbstractAction
implements FocusListener, MouseListener, UIResource
Class Inheritance
All Implemented Interfaces
javax.swing.plaf.UIResource, java.awt.event.MouseListener, java.util.EventListener, java.awt.event.FocusListener
Annotations
@SuppressWarnings:serial

A handler for spinner arrow button mouse and action events. When a left mouse pressed event occurs we look up the (enabled) spinner that's the source of the event and start the autorepeat timer. The timer fires action events until any button is released at which point the timer is stopped and the reference to the spinner cleared. The timer doesn't start until after a 300ms delay, so often the source of the initial (and final) action event is just the button logic for mouse released - which means that we're relying on the fact that our mouse listener runs after the buttons mouse listener.

Note that one instance of this handler is shared by all slider previous arrow buttons and likewise for all of the next buttons, so it doesn't have any state that persists beyond the limits of a single button pressed/released gesture.

Field Summary

Modifier and TypeField and Description
pack-priv JButton
pack-priv final Timer
pack-priv final boolean
pack-priv JSpinner
Inherited from javax.swing.AbstractAction:
changeSupportenabled

Constructor Summary

AccessConstructor and Description
pack-priv
ArrowButtonHandler(String name, boolean isNext)

Method Summary

Modifier and TypeMethod and Description
public void
actionPerformed(ActionEvent e)

Implements java.awt.event.ActionListener.actionPerformed.

Invoked when an action occurs.

private JSpinner
public void
focusGained(FocusEvent e)

Implements java.awt.event.FocusListener.focusGained.

Invoked when a component gains the keyboard focus.

public void
focusLost(FocusEvent e)

Implements java.awt.event.FocusListener.focusLost.

Invoked when a component loses the keyboard focus.

private void
focusSpinnerIfNecessary()

Requests focus on a child of the spinner if the spinner doesn't have focus.

private int
getCalendarField(JSpinner spinner)

Returns the calendarField under the start of the selection, or -1 if there is no valid calendar field under the selection (or the spinner isn't editing dates.

public void
mouseClicked(MouseEvent e)

Implements java.awt.event.MouseListener.mouseClicked.

Invoked when the mouse button has been clicked (pressed and released) on a component.

public void
mouseEntered(MouseEvent e)

Implements java.awt.event.MouseListener.mouseEntered.

Invoked when the mouse enters a component.

public void
mouseExited(MouseEvent e)

Implements java.awt.event.MouseListener.mouseExited.

Invoked when the mouse exits a component.

public void
mousePressed(MouseEvent e)

Implements java.awt.event.MouseListener.mousePressed.

Invoked when a mouse button has been pressed on a component.

public void
mouseReleased(MouseEvent e)

Implements java.awt.event.MouseListener.mouseReleased.

Invoked when a mouse button has been released on a component.

private void
select(JSpinner spinner)

If the spinner's editor is a DateEditor, this selects the field associated with the value that is being incremented.

private boolean
select(JFormattedTextField ftf, AttributedCharacterIterator iterator, DateFormat.Field field)

Selects the passed in field, returning true if it is found, false otherwise.

Inherited from javax.swing.AbstractAction:
addPropertyChangeListenerclonefirePropertyChangegetKeysgetPropertyChangeListenersgetValueisEnabledputValueremovePropertyChangeListenersetEnabled

Field Detail

arrowButtonback to summary
pack-priv JButton arrowButton
autoRepeatTimerback to summary
pack-priv final Timer autoRepeatTimer
isNextback to summary
pack-priv final boolean isNext
spinnerback to summary
pack-priv JSpinner spinner

Constructor Detail

ArrowButtonHandlerback to summary
pack-priv ArrowButtonHandler(String name, boolean isNext)

Method Detail

actionPerformedback to summary
public void actionPerformed(ActionEvent e)

Implements java.awt.event.ActionListener.actionPerformed.

Doc from java.awt.event.ActionListener.actionPerformed.

Invoked when an action occurs.

eventToSpinnerback to summary
private JSpinner eventToSpinner(AWTEvent e)
focusGainedback to summary
public void focusGained(FocusEvent e)

Implements java.awt.event.FocusListener.focusGained.

Doc from java.awt.event.FocusListener.focusGained.

Invoked when a component gains the keyboard focus.

focusLostback to summary
public void focusLost(FocusEvent e)

Implements java.awt.event.FocusListener.focusLost.

Doc from java.awt.event.FocusListener.focusLost.

Invoked when a component loses the keyboard focus.

focusSpinnerIfNecessaryback to summary
private void focusSpinnerIfNecessary()

Requests focus on a child of the spinner if the spinner doesn't have focus.

getCalendarFieldback to summary
private int getCalendarField(JSpinner spinner)

Returns the calendarField under the start of the selection, or -1 if there is no valid calendar field under the selection (or the spinner isn't editing dates.

mouseClickedback to summary
public void mouseClicked(MouseEvent e)

Implements java.awt.event.MouseListener.mouseClicked.

Doc from java.awt.event.MouseListener.mouseClicked.

Invoked when the mouse button has been clicked (pressed and released) on a component.

mouseEnteredback to summary
public void mouseEntered(MouseEvent e)

Implements java.awt.event.MouseListener.mouseEntered.

Doc from java.awt.event.MouseListener.mouseEntered.

Invoked when the mouse enters a component.

mouseExitedback to summary
public void mouseExited(MouseEvent e)

Implements java.awt.event.MouseListener.mouseExited.

Doc from java.awt.event.MouseListener.mouseExited.

Invoked when the mouse exits a component.

mousePressedback to summary
public void mousePressed(MouseEvent e)

Implements java.awt.event.MouseListener.mousePressed.

Doc from java.awt.event.MouseListener.mousePressed.

Invoked when a mouse button has been pressed on a component.

mouseReleasedback to summary
public void mouseReleased(MouseEvent e)

Implements java.awt.event.MouseListener.mouseReleased.

Doc from java.awt.event.MouseListener.mouseReleased.

Invoked when a mouse button has been released on a component.

selectback to summary
private void select(JSpinner spinner)

If the spinner's editor is a DateEditor, this selects the field associated with the value that is being incremented.

selectback to summary
private boolean select(JFormattedTextField ftf, AttributedCharacterIterator iterator, DateFormat.Field field)

Selects the passed in field, returning true if it is found, false otherwise.

javax.swing.plaf.basic back to summary

private Class BasicSpinnerUI.Handler

extends Object
implements LayoutManager, PropertyChangeListener, ChangeListener
Class Inheritance
All Implemented Interfaces
javax.swing.event.ChangeListener, java.util.EventListener, java.beans.PropertyChangeListener, java.awt.LayoutManager

Field Summary

Modifier and TypeField and Description
private Component
private Component
private Component

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
addLayoutComponent(String name, Component c)

Implements java.awt.LayoutManager.addLayoutComponent.

If the layout manager uses a per-component string, adds the component comp to the layout, associating it with the string specified by name.

public void
layoutContainer(Container parent)

Implements java.awt.LayoutManager.layoutContainer.

Lays out the specified container.

public Dimension
minimumLayoutSize(Container parent)

Implements java.awt.LayoutManager.minimumLayoutSize.

Calculates the minimum size dimensions for the specified container, given the components it contains.

public Dimension
preferredLayoutSize(Container parent)

Implements java.awt.LayoutManager.preferredLayoutSize.

Calculates the preferred size dimensions for the specified container, given the components it contains.

private Dimension
public void
propertyChange(PropertyChangeEvent e)

Implements java.beans.PropertyChangeListener.propertyChange.

This method gets called when a bound property is changed.

public void
removeLayoutComponent(Component c)

Implements java.awt.LayoutManager.removeLayoutComponent.

Removes the specified component from the layout.

private void
setBounds(Component c, int x, int y, int width, int height)

public void
stateChanged(ChangeEvent e)

Implements javax.swing.event.ChangeListener.stateChanged.

Invoked when the target of the listener has changed its state.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

editorback to summary
private Component editor
nextButtonback to summary
private Component nextButton
previousButtonback to summary
private Component previousButton

Constructor Detail

Handlerback to summary
private Handler()

Method Detail

addLayoutComponentback to summary
public void addLayoutComponent(String name, Component c)

Implements java.awt.LayoutManager.addLayoutComponent.

Doc from java.awt.LayoutManager.addLayoutComponent.

If the layout manager uses a per-component string, adds the component comp to the layout, associating it with the string specified by name.

layoutContainerback to summary
public void layoutContainer(Container parent)

Implements java.awt.LayoutManager.layoutContainer.

Doc from java.awt.LayoutManager.layoutContainer.

Lays out the specified container.

minimumLayoutSizeback to summary
public Dimension minimumLayoutSize(Container parent)

Implements java.awt.LayoutManager.minimumLayoutSize.

Doc from java.awt.LayoutManager.minimumLayoutSize.

Calculates the minimum size dimensions for the specified container, given the components it contains.

preferredLayoutSizeback to summary
public Dimension preferredLayoutSize(Container parent)

Implements java.awt.LayoutManager.preferredLayoutSize.

Doc from java.awt.LayoutManager.preferredLayoutSize.

Calculates the preferred size dimensions for the specified container, given the components it contains.

preferredSizeback to summary
private Dimension preferredSize(Component c)
propertyChangeback to summary
public void propertyChange(PropertyChangeEvent e)

Implements java.beans.PropertyChangeListener.propertyChange.

Doc from java.beans.PropertyChangeListener.propertyChange.

This method gets called when a bound property is changed.

removeLayoutComponentback to summary
public void removeLayoutComponent(Component c)

Implements java.awt.LayoutManager.removeLayoutComponent.

Doc from java.awt.LayoutManager.removeLayoutComponent.

Removes the specified component from the layout.

setBoundsback to summary
private void setBounds(Component c, int x, int y, int width, int height)
stateChangedback to summary
public void stateChanged(ChangeEvent e)

Implements javax.swing.event.ChangeListener.stateChanged.

Doc from javax.swing.event.ChangeListener.stateChanged.

Invoked when the target of the listener has changed its state.

updateToolTipTextForChildrenback to summary
private void updateToolTipTextForChildren(JComponent spinner)