Class IntegerStringConverter

java.lang.Object
javafx.util.StringConverter<Integer>
atlantafx.base.util.IntegerStringConverter

public class IntegerStringConverter extends javafx.util.StringConverter<Integer>
Converts between user-edited strings and integer 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.

Author:
Christoph Nahr
  • Constructor Details

    • IntegerStringConverter

      public IntegerStringConverter()
      Creates an IntegerStringConverter. Swallows NumberFormatException but does nothing in response until setReset(java.lang.Runnable) is defined.
    • IntegerStringConverter

      public IntegerStringConverter(Runnable reset)
      Creates an IntegerStringConverter with an editor reset callback. Specifying null has the same effect as the default constructor.
      Parameters:
      reset - The Runnable to call upon NumberFormatException.
    • IntegerStringConverter

      public IntegerStringConverter(javafx.scene.control.TextField input, int min, int max)
      Creates an IntegerStringConverter 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 integer value.
      max - The greatest valid integer value.
      Throws:
      NullPointerException - if input is null
  • Method Details

    • createFor

      public static IntegerStringConverter createFor(javafx.scene.control.Spinner<Integer> spinner)
      Creates an IntegerStringConverter for the specified Spinner. Uses the TextField and minimum and maximum values of the specified Spinner for construction, and also sets the new IntegerStringConverter on its SpinnerValueFactory.IntegerSpinnerValueFactory.
      Parameters:
      spinner - The Spinner to create an IntegerStringConverter for.
      Returns:
      the new IntegerStringConverter
      Throws:
      NullPointerException - if spinner is null
    • setReset

      public void setReset(Runnable reset)
      Sets the editor reset callback.

      Specify null to clear a previously set Runnable. When creating an IntegerStringConverter 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 Integer fromString(String s)
      Converts the specified string into its integer 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<Integer>
      Parameters:
      s - The String to convert.
      Returns:
      the integer value of s
      See Also:
    • toString

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