JavaFX bind to multiple properties

前端 未结 3 557
忘了有多久
忘了有多久 2020-12-15 06:26

I have a simple fxml with a textfield and a button. I\'d like to have the button disabled if the textfield is empty. So I insert something like the following in my controlle

相关标签:
3条回答
  • 2020-12-15 07:06

    In addition to martin_dk answer, if you want to bind more than two properties you will get code like below, looks weird, but it works.

    BooleanBinding booleanBinding
            = finalEditor.selectedProperty().or(
                    staticEditor.selectedProperty().or(
                            syncEditor.selectedProperty().or(
                                    nativeEditor.selectedProperty().or(
                                            strictEditor.selectedProperty()))));
    
    abstractEditor.disableProperty ().bind(booleanBinding);
    
    0 讨论(0)
  • 2020-12-15 07:24

    This is possible by binding to a boolean expression via Bindings:

    button.disableProperty().bind(
        Bindings.and(
            textField.textProperty().isEqualTo(""),
            textField2.textProperty().isEqualTo("")));
    
    0 讨论(0)
  • 2020-12-15 07:30

    In addition to Andreys approach, I found that you can also do it like this:

        BooleanBinding booleanBinding = 
          textField.textProperty().isEqualTo("").or(
            textField2.textProperty().isEqualTo(""));
    
        button.disableProperty().bind(booleanBinding);
    
    0 讨论(0)
提交回复
热议问题