How to disable Button when TextField is empty?

后端 未结 3 1972
暖寄归人
暖寄归人 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 07:03

    use textProperty() Listener for TextField

    try this...

    Button b1 = new Button("DELETE");
    b1.setFont(Font.font("Calibri", FontWeight.BOLD, 17));
    b1.setPrefSize(100, 30);
    b1.setStyle(" -fx-base: #ffffff;");
    b1.setTextFill(Color.BLACK);
    
    b1.setDisable(true); // Initally text box was empty so button was disable
    
    txt1.textProperty().addListener(new ChangeListener() {
    
            @Override
            public void changed(ObservableValue ov, String t, String t1) {
                //System.out.println(t+"====="+t1);
               if(t1.equals(""))
                   b1.setDisable(true);
               else
                   b1.setDisable(false);
            }
        });
    

提交回复
热议问题