Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants