Class DoubleStringConverter

java.lang.Object
javafx.util.StringConverter<Double>
atlantafx.base.util.DoubleStringConverter

public class DoubleStringConverter extends javafx.util.StringConverter<Double>
Converts between user-edited strings and Double values.

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 Details

    • DoubleStringConverter

      public DoubleStringConverter()
      Creates a DoubleStringConverter.

      Swallows NumberFormatException but does nothing in response until setReset(java.lang.Runnable) is defined.

    • DoubleStringConverter

      public DoubleStringConverter(Runnable reset)
      Creates a DoubleStringConverter with an editor reset callback. Specifying null has the same effect as the default constructor.
      Parameters:
      reset - the Runnable to call upon NumberFormatException
    • 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 is null.
  • Method Details

    • createFor

      public static DoubleStringConverter createFor(javafx.scene.control.Spinner<Double> spinner)
      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 is null
    • setReset

      public void setReset(Runnable reset)
      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

      public Double fromString(String s)
      Converts the specified string into its double value. A null, empty, or otherwise invalid argument returns zero and also executes the editor reset callback, if any.
      Specified by:
      fromString in class javafx.util.StringConverter<Double>
      Parameters:
      s - The string to convert.
      Returns:
      the double value of s
      See Also:
    • toString

      public String toString(Double value)
      Converts the specified double into its string form. A null argument is converted into the literal string "0".
      Specified by:
      toString in class javafx.util.StringConverter<Double>
      Parameters:
      value - The Double to convert.
      Returns:
      the string form of value