Home > Software design >  Why are the javafx Input checks not working in scala
Why are the javafx Input checks not working in scala

Time:06-03

How to perform input checks in TextInputDialog in JavaFX?

The above link basically covers the answer to my question but it doesn't work for me in scala. I have a TextField (eTextField) and I want to check that it contains either a R or D. If it does than the button (ePR_ePD) should be enabled. Here is the part of my code that should do it, but it doesn't enable the button:

      val isInvalid: BooleanBinding = Bindings.createBooleanBinding(() => !isValid(eTextField.getText, eTextField.textProperty))
      println(isInvalid)
      ePR_ePD.disableProperty().bind(isInvalid)

def isValid (eText: String, eTextProp: StringProperty): Boolean = {
    println(eText.contains("R")||eText.contains("D"))
    eText.contains("R")||eText.contains("D")
}

When the eTextField contains a R or D my output is:

BooleanBinding [invalid]
true

and when it does not contain either my output is:

BooleanBinding [invalid]
false

But in both cases the ePR_ePD button says disabled. It appears it is something to do with the BooleanBinding, since it is always invalid.

Also not sure why isValid(eTextField.getText, eTextField.textProperty) needs to have the textProperty parameter, but that is what the answer in the link showed. Also tried "var isInvalid..." but that didn't help.

CodePudding user response:

According to the documentation and as noted in the comments, createBooleanBinding takes two arguments, the function and the observable.

The function just tests the property value so isValid does not need the second argument. (The clue is that the second argument is not actually used)

def isValid (eText: String): Boolean = 
    eText.contains("R")|| eText.contains("D")

The property itself is passed as a second parameter to createBooleanBinding rather than to isValid:

createBooleanBinding(() => !isValid(eTextField.getText), eTextField.textProperty)

CodePudding user response:

This works, after I fixed the problem that I forgot to enter the fx:id in SceneBuilder for the ePR_ePD button:

(In the initialize() method of the fxml controller:)

   val isInvalid: BooleanBinding = Bindings.createBooleanBinding( () => !(isValid(eTextField.getText)), eTextField.textProperty)
    ePR_ePD.disableProperty().bind(isInvalid)
  }

(Outside the initialize() method:)

      def isValid (eText: String): Boolean = {
        println(eText.contains("R")||eText.contains("D"))
        eText.contains("R")||eText.contains("D")
      }

The only odd thing is that it appears to call isValid twice when I use another method to automatically change the contents of the eTextField. My output is false false when there is no R or D in the field and true true when either is present.

This also works, without an isValid method:

(In the initialize() method of the fxml controller:)

       val isInvalid: BooleanBinding = Bindings.createBooleanBinding(
        () => !(eTextField.getText.contains("R")||eTextField.getText.contains("D")), eTextField.textProperty
       )

ePR_ePD.disableProperty().bind(isInvalid)

Thanks to everyone for your help!

  • Related