Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

A custom double textfield #1341

Open
AIGLEZMA opened this issue Sep 3, 2021 · 2 comments
Open

A custom double textfield #1341

AIGLEZMA opened this issue Sep 3, 2021 · 2 comments

Comments

@AIGLEZMA
Copy link

AIGLEZMA commented Sep 3, 2021

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)
@zentox
Copy link

zentox commented Sep 16, 2021

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)

@SchweinchenFuntik
Copy link
Contributor

SchweinchenFuntik commented Sep 17, 2021

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants