tornadofx icon indicating copy to clipboard operation
tornadofx copied to clipboard

A custom double textfield

Open AIGLEZMA opened this issue 3 years ago • 2 comments

Hey, i'm new to JavaFX and TornadoFX so i have some questions:

I want to make a double only textfield and bind it to a double property and i want to know how to:

  • Make the textfield only accept doubles to prevent the java.lang.NumberFormatException
  • Accept the - char and do not throw java.lang.NumberFormatException
  • Accept , as a decimal separator
  • Make the textfield show nothing by default (cause when you bind it to a double property it will show 0 by default)

AIGLEZMA avatar Sep 03 '21 14:09 AIGLEZMA

Make the textfield only accept doubles to prevent the java.lang.NumberFormatException Accept , as a decimal separator

You can use a TextFormatter .

Accept the - char and do not throw java.lang.NumberFormatException

You probably have to specify a double value for it. Maybe use something as dummy value e.g. Double.NaN

Make the textfield show nothing by default (cause when you bind it to a double property it will show 0 by default)

You can override the NumberStringConverter toString method in order to achieve this.

Minimal example code:

        val textField = TextField()
        textField.textFormatter = ...
        val doubleProperty = SimpleDoubleProperty(0.0)
        val converter = ...
        Bindings.bindBidirectional(textField.textProperty(), doubleProperty, converter)

zentox avatar Sep 16 '21 21:09 zentox

val doubleProperty = doubleProperty(0.0)
textfield(doubleProperty).validator {
    if (it?.toDoubleOrNull() == null) error("Not correct Double Number")
    else success() // or null
}

SchweinchenFuntik avatar Sep 17 '21 15:09 SchweinchenFuntik