Class IntegerTextField
This field does continuous checking, so you can't enter a bad value.
Internally, values are maintained using BigIntegers so this field can contain numbers as large as
desired. There are convenience methods for getting the value as either an int or long. If using
these convenience methods, you should also set the max allowed value so that users can't enter a
value larger than can be represented by the getIntValue()
or getLongValue()
methods as appropriate.
There are several configuration options as follows:
- Allows negative numbers - either support all integer numbers or just non-negative numbers.
See
setAllowNegativeValues(boolean)
- Allows hex prefix - If this mode is on, then hex mode is turned on and off automatically
depending whether or not the text starts with 0x. Otherwise, the hex/decimal mode is set
externally (either programmatically or pressing <CTRL> M) and the user is restricted to the
numbers/letters appropriate for that mode. See
setAllowsHexPrefix(boolean)
- Have a max value - a max value can be set (must be positive) such that the user can not type
a number whose absolute value is greater than the max. Otherwise, the value is unlimited if max
is null/unspecified. See
setMaxValue(BigInteger)
- Show the number mode as hint text - If on either "Hex" or "Dec" is displayed lightly in the
bottom right portion of the text field. See
setShowNumberMode(boolean)
-
Constructor Summary
ConstructorDescriptionCreates a new IntegerTextField with 5 columns and no initial valueIntegerTextField
(int columns) Creates a new IntegerTextField with the specified number of columns and no initial valueIntegerTextField
(int columns, long initialValue) Creates a new IntegerTextField with the specified number of columns and an initial valueIntegerTextField
(int columns, BigInteger initialValue) Creates a new IntegerTextField with the specified number of columns and initial value -
Method Summary
Modifier and TypeMethodDescriptionvoid
addActionListener
(ActionListener listener) Adds an ActionListener to the TextField.void
addChangeListener
(ChangeListener listener) Adds a change listener that will be notified whenever the value changes.Returns the JTextField component that this class manages.int
Returns the current value as an int.long
Returns the current value as a long.Returns the current maximum allowed value.getText()
Returns the current text displayed in the field.getValue()
Returns the current value of the field or null if the field has no current value.boolean
Returns true if in hex mode, false if in decimal mode.void
removeActionListener
(ActionListener listener) Removes an ActionListener from the TextField.void
removeChangeListener
(ChangeListener listener) Removes the changes listener.void
Requests focus to the JTextFieldvoid
Selects the text in the JTextFieldvoid
setAccessibleName
(String name) Sets the accessible name for the component of this input field.void
setAllowNegativeValues
(boolean b) Sets whether or not negative numbers are accepted.void
setAllowsHexPrefix
(boolean allowsHexPrefix) Sets whether on not the field supports the 0x prefix.void
Sets the mode to Decimal.void
setEditable
(boolean editable) Sets the editable mode for the JTextField componentvoid
setEnabled
(boolean enabled) Sets the enablement on the JTextField component;void
Sets the radix mode to Hex.void
setHorizontalAlignment
(int alignment) Sets the horizontal alignment of the JTextFieldvoid
setMaxValue
(BigInteger maxValue) Sets the maximum allowed value.void
setMinValue
(BigInteger minValue) Sets the minimum allowed value.void
setShowNumberMode
(boolean show) Turns on or off the faded text that displays the field's radix mode (hex or decimal).boolean
Sets the field to the given text.void
setValue
(int newValue) Convenience method for setting the value to an int value;void
setValue
(long newValue) Convenience method for setting the value to a long value;void
setValue
(BigInteger newValue) Sets the value of the field to the given value.
-
Constructor Details
-
IntegerTextField
public IntegerTextField()Creates a new IntegerTextField with 5 columns and no initial value -
IntegerTextField
public IntegerTextField(int columns) Creates a new IntegerTextField with the specified number of columns and no initial value- Parameters:
columns
- the number of columns.
-
IntegerTextField
public IntegerTextField(int columns, long initialValue) Creates a new IntegerTextField with the specified number of columns and an initial value- Parameters:
columns
- the number of columns to display in the JTextField.initialValue
- the initial value. This constructor takes an initialValue as a long. If you need a value that is bigger (or smaller) than can be specified as a long, then use the constructor that takes a BigInteger as an initial value.
-
IntegerTextField
Creates a new IntegerTextField with the specified number of columns and initial value- Parameters:
columns
- the number of columnsinitialValue
- the initial value
-
-
Method Details
-
addChangeListener
Adds a change listener that will be notified whenever the value changes.- Parameters:
listener
- the change listener to add.
-
setAccessibleName
Sets the accessible name for the component of this input field.- Parameters:
name
- the accessible name for this field
-
removeChangeListener
Removes the changes listener.- Parameters:
listener
- the listener to be removed.
-
getValue
Returns the current value of the field or null if the field has no current value.- Returns:
- the current value of the field or null if the field has no current value.
-
getIntValue
public int getIntValue()Returns the current value as an int.If the field has no current value, 0 will be returned. If the value is bigger (or smaller) than an int, it will be cast to an int.
If using this method, it is highly recommended that you set the max value to
Integer.MAX_VALUE
or lower.- Returns:
- the current value as an int. Or 0 if there is no value
- Throws:
ArithmeticException
- if the value in this field will not fit into an int
-
getLongValue
public long getLongValue()Returns the current value as a long.If the field has no current value, 0 will be returned. If the value is bigger (or smaller) than an long, it will be cast to a long.
If using this method, it is highly recommended that you set the max value to
Long.MAX_VALUE
or lower.- Returns:
- the current value as a long. Or 0 if there is no value
- Throws:
ArithmeticException
- if the value in this field will not fit into a long
-
setValue
public void setValue(long newValue) Convenience method for setting the value to a long value;- Parameters:
newValue
- the new value for the field.
-
setValue
public void setValue(int newValue) Convenience method for setting the value to an int value;- Parameters:
newValue
- the new value for the field.
-
setText
Sets the field to the given text. The text must be a properly formated string that is a value that is valid for this field. If the field is set to not allow "0x" prefixes, then the input string cannot start with 0x and furthermore, if the field is in decimal mode, then input string cannot take in hex digits a-f. On the other hand, if "0x" prefixes are allowed, then the input string can be either a decimal number or a hex number depending on if the input string starts with "0x". In this case, the field's hex mode will be set to match the input text. If the text is not valid, the field will not change.- Parameters:
text
- the value as text to set on this field- Returns:
- true if the set was successful
-
setValue
Sets the value of the field to the given value. A null value will clear the field.- Parameters:
newValue
- the new value or null.
-
setShowNumberMode
public void setShowNumberMode(boolean show) Turns on or off the faded text that displays the field's radix mode (hex or decimal).- Parameters:
show
- true to show the radix mode.
-
setHexMode
public void setHexMode()Sets the radix mode to Hex.If the field is currently in decimal mode, the current text will be change from displaying the current value from decimal to hex.
-
setDecimalMode
public void setDecimalMode()Sets the mode to Decimal.If the field is currently in hex mode, the current text will be change from displaying the current value from hex to decimal.
-
setAllowsHexPrefix
public void setAllowsHexPrefix(boolean allowsHexPrefix) Sets whether on not the field supports the 0x prefix.If 0x is supported, hex numbers will be displayed with the 0x prefix. Also, when typing, you must type 0x first to enter a hex number, otherwise it will only allow digits 0-9. If the 0x prefix option is turned off, then hex numbers are displayed without the 0x prefix and you can't change the decimal/hex mode by typing 0x. The field will either be in decimal or hex mode and the typed text will be interpreted appropriately for the mode.
- Parameters:
allowsHexPrefix
- true to use the 0x convention for hex.
-
getText
Returns the current text displayed in the field.- Returns:
- the current text displayed in the field.
-
isHexMode
public boolean isHexMode()Returns true if in hex mode, false if in decimal mode.- Returns:
- true if in hex mode, false if in decimal mode.
-
setAllowNegativeValues
public void setAllowNegativeValues(boolean b) Sets whether or not negative numbers are accepted.- Parameters:
b
- if true, negative numbers are allowed.
-
getMaxValue
Returns the current maximum allowed value. Null indicates that there is no maximum value. If negative values are permitted (seesetAllowNegativeValues(boolean)
) this value will establish the upper and lower limit of the absolute value.- Returns:
- the current maximum value allowed.
-
setMaxValue
Sets the maximum allowed value. The maximum must be a positive number. Null indicates that there is no maximum value.If negative values are permitted (see
setAllowNegativeValues(boolean)
) this value will establish the upper and lower limit of the absolute value.- Parameters:
maxValue
- the maximum value to allow.
-
setMinValue
Sets the minimum allowed value. The minimum must be a positive number. Null indicates that there is no minimum value.If negative values are permitted (see
setAllowNegativeValues(boolean)
) this value will establish the minimum limit of the absolute value.- Parameters:
minValue
- the minimum value to allow.
-
getComponent
Returns the JTextField component that this class manages.- Returns:
- the JTextField component that this class manages.
-
addActionListener
Adds an ActionListener to the TextField.- Parameters:
listener
- the ActionListener to add.
-
removeActionListener
Removes an ActionListener from the TextField.- Parameters:
listener
- the ActionListener to remove.
-
setEnabled
public void setEnabled(boolean enabled) Sets the enablement on the JTextField component;- Parameters:
enabled
- true for enabled, false for disabled.
-
setEditable
public void setEditable(boolean editable) Sets the editable mode for the JTextField component- Parameters:
editable
- boolean flag, if true component is editable
-
requestFocus
public void requestFocus()Requests focus to the JTextField -
selectAll
public void selectAll()Selects the text in the JTextField -
setHorizontalAlignment
public void setHorizontalAlignment(int alignment) Sets the horizontal alignment of the JTextField- Parameters:
alignment
- the alignment as inJTextField.setHorizontalAlignment(int)
-