Accepts an optional Runnable that resets the editor on NumberFormatException
,
or a TextField or Spinner that is preemptively monitored for invalid input
during typing, and restricts valid input to a specified range when committed.
This implementation shows up to two decimal digits, but only if a fractional part exists. The default implementation always shows one decimal digit which hinders typing.
- Author:
- Christoph Nahr
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a DoubleStringConverter.DoubleStringConverter
(Runnable reset) Creates a DoubleStringConverter with an editor reset callback.DoubleStringConverter
(javafx.scene.control.TextField input, double min, double max) Creates a DoubleStringConverter with the specified input range. -
Method Summary
Modifier and TypeMethodDescriptionstatic DoubleStringConverter
Creates a DoubleStringConverter for the specified Spinner.fromString
(String s) Converts the specified string into its double value.void
Sets the editor reset callback.Converts the specified double into its string form.
-
Constructor Details
-
DoubleStringConverter
public DoubleStringConverter()Creates a DoubleStringConverter.Swallows
NumberFormatException
but does nothing in response untilsetReset(java.lang.Runnable)
is defined. -
DoubleStringConverter
Creates a DoubleStringConverter with an editor reset callback. Specifying null has the same effect as the default constructor.- Parameters:
reset
- the Runnable to call uponNumberFormatException
-
DoubleStringConverter
public DoubleStringConverter(javafx.scene.control.TextField input, double min, double max) Creates a DoubleStringConverter with the specified input range.Preemptively monitors input to reject any invalid characters during typing. Restricts input to [
min
,max
] (inclusive) when valid text is committed, and resets input to the closest value to zero within [min
,max
] when invalid text is committed.- Parameters:
input
- The TextField providing user-edited strings.min
- The smallest valid value.max
- The greatest valid value.- Throws:
NullPointerException
- if input isnull
.
-
-
Method Details
-
createFor
Creates a DoubleStringConverter for the specified Spinner.Uses the TextField and minimum and maximum values of the specified Spinner for construction, and also sets the new DoubleStringConverter on its
SpinnerValueFactory.DoubleSpinnerValueFactory
.- Parameters:
spinner
- The Spinner to create a DoubleStringConverter for.- Returns:
- the new DoubleStringConverter
- Throws:
NullPointerException
- if the Spinner isnull
-
setReset
Sets the editor reset callback.Specify
null
to clear a previously set Runnable. When creating a DoubleStringConverter for a TextField or Spinner, this callback is automatically defined to reset committed invalid input to the closest value to zero within the legal range. Setting a different callback will overwrite this functionality.- Parameters:
reset
- The Runnable to call upon NumberFormatException.- See Also:
-
fromString
Converts the specified string into its double value. Anull
, empty, or otherwise invalid argument returns zero and also executes the editor reset callback, if any.- Specified by:
fromString
in classjavafx.util.StringConverter<Double>
- Parameters:
s
- The string to convert.- Returns:
- the double value of
s
- See Also:
-
toString
-