com.toedter.components

Class JSpinField

Implemented Interfaces:
ActionListener, CaretListener, ChangeListener, FocusListener
Known Direct Subclasses:
JYearChooser

public class JSpinField
extends JPanel
implements ChangeListener, CaretListener, ActionListener, FocusListener

JSpinField is a numeric field with 2 spin buttons to increase or decrease the value. It has the same interface as the "old" JSpinField but uses a JSpinner internally (since J2SE SDK 1.4) rather than a scrollbar for emulating the spin buttons.
Version:
$LastChangedRevision: 85 $, $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $
Author:
Kai Toedter

Field Summary

protected Color
darkGreen
protected int
max
protected int
min
protected JSpinner
spinner
protected JTextField
textField
the text (number) field
protected int
value

Constructor Summary

JSpinField()
Default JSpinField constructor.
JSpinField(int min, int max)
JSpinField constructor with given minimum and maximum vaues and initial value 0.

Method Summary

void
actionPerformed(ActionEvent e)
After any user input, the value of the textfield is proofed.
void
adjustWidthToMaximumValue()
void
caretUpdate(CaretEvent e)
After any user input, the value of the textfield is proofed.
void
focusGained(FocusEvent e)
void
focusLost(FocusEvent e)
The value of the text field is checked against a valid (green) value.
int
getMaximum()
Returns the maximum value.
int
getMinimum()
Returns the minimum value.
Component
getSpinner()
Returns the year chooser's spinner (which allow the focus to be set to it).
int
getValue()
Returns the value.
static void
main(String[] s)
Creates a JFrame with a JSpinField inside and can be used for testing.
void
setEnabled(boolean enabled)
Enable or disable the JSpinField.
void
setFont(Font font)
Sets the font property.
void
setForeground(Color fg)
Sets the foreground
void
setHorizontalAlignment(int alignment)
Sets the horizontal alignment of the displayed value.
void
setMaximum(int newMaximum)
Sets the maximum value and adjusts the preferred width.
void
setMinimum(int newMinimum)
Sets the minimum value.
void
setValue(int newValue)
Sets the value.
protected void
setValue(int newValue, boolean updateTextField, boolean firePropertyChange)
Sets the value attribute of the JSpinField object.
void
stateChanged(ChangeEvent e)
Is invoked when the spinner model changes

Field Details

darkGreen

protected Color darkGreen

max

protected int max

min

protected int min

spinner

protected JSpinner spinner

textField

protected JTextField textField
the text (number) field

value

protected int value

Constructor Details

JSpinField

public JSpinField()
Default JSpinField constructor. The valid value range is between Integer.MIN_VALUE and Integer.MAX_VALUE. The initial value is 0.

JSpinField

public JSpinField(int min,
                  int max)
JSpinField constructor with given minimum and maximum vaues and initial value 0.

Method Details

actionPerformed

public void actionPerformed(ActionEvent e)
After any user input, the value of the textfield is proofed. Depending on being an integer, the value is colored green or red. If the textfield is green, the enter key is accepted and the new value is set.
Parameters:
e - Description of the Parameter

adjustWidthToMaximumValue

public void adjustWidthToMaximumValue()

caretUpdate

public void caretUpdate(CaretEvent e)
After any user input, the value of the textfield is proofed. Depending on being an integer, the value is colored green or red.
Parameters:
e - the caret event

focusGained

public void focusGained(FocusEvent e)

focusLost

public void focusLost(FocusEvent e)
The value of the text field is checked against a valid (green) value. If valid, the value is set and a property change is fired.

getMaximum

public int getMaximum()
Returns the maximum value.
Returns:
the maximum value

getMinimum

public int getMinimum()
Returns the minimum value.
Returns:
the minimum value

getSpinner

public Component getSpinner()
Returns the year chooser's spinner (which allow the focus to be set to it).
Returns:
Component the spinner or null, if the month chooser has no spinner

getValue

public int getValue()
Returns the value.
Returns:
the value value

main

public static void main(String[] s)
Creates a JFrame with a JSpinField inside and can be used for testing.
Parameters:
s - The command line arguments

setEnabled

public void setEnabled(boolean enabled)
Enable or disable the JSpinField.
Parameters:
enabled - The new enabled value

setFont

public void setFont(Font font)
Sets the font property.
Parameters:
font - the new font

setForeground

public void setForeground(Color fg)
Sets the foreground
Parameters:
fg - the foreground

setHorizontalAlignment

public void setHorizontalAlignment(int alignment)
Sets the horizontal alignment of the displayed value.
Parameters:
alignment - the horizontal alignment

setMaximum

public void setMaximum(int newMaximum)
Sets the maximum value and adjusts the preferred width.
Parameters:
newMaximum - the new maximum value

setMinimum

public void setMinimum(int newMinimum)
Sets the minimum value.
Parameters:
newMinimum - the new minimum value

setValue

public void setValue(int newValue)
Sets the value. This is a bound property.
Parameters:
newValue - the new value

setValue

protected void setValue(int newValue,
                        boolean updateTextField,
                        boolean firePropertyChange)
Sets the value attribute of the JSpinField object.
Parameters:
newValue - The new value
updateTextField - true if text field should be updated

stateChanged

public void stateChanged(ChangeEvent e)
Is invoked when the spinner model changes
Parameters:
e - the ChangeEvent

Copyright B) 1999 - 2009 Kai Toedter.