How to disable Button when TextField is empty?

后端 未结 3 1959
暖寄归人
暖寄归人 2020-11-27 06:03

In the following code I have a TextField and a Button. I need to disable the Button when ever the TextField is empty, so that I can avoid entering empty values to the databa

3条回答
  •  春和景丽
    2020-11-27 06:51

    The other way can be using bindings:

    final TextField textField1 = new TextField();
    final TextField textField2 = new TextField();
    final TextField textField3 = new TextField();
    
    BooleanBinding bb = new BooleanBinding() {
        {
            super.bind(textField1.textProperty(),
                    textField2.textProperty(),
                    textField3.textProperty());
        }
    
        @Override
        protected boolean computeValue() {
            return (textField1.getText().isEmpty()
                    && textField2.getText().isEmpty()
                    && textField3.getText().isEmpty());
        }
    };
    
    Button btn = new Button("Button");
    btn.disableProperty().bind(bb);
    
    VBox vBox = new VBox();
    vBox.getChildren().addAll(textField1, textField2, textField3, btn);
    

提交回复
热议问题