如果TextField为空,如何禁用按钮? [英] How to disable button if the TextField empty?
本文介绍了如果TextField为空,如何禁用按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法禁用我的按钮.在下面的代码中, accept
是 Button
,而 email
是> TextField
.
I can't disable my button. In the below code, accept
is a Button
and email
is a TextField
.
email.setOnAction(ae -> {
if(!email.getText().isEmpty()) {
accept.setDisable(false);
} else
accept.setDisable(true);
});
如果我在文本字段中写,它什么也没做.
It doesn't do anything if I write in the text field.
推荐答案
按照Zephyr的回答,您可以将其直接绑定到您的按钮内:
Following Zephyr's answer, you can directly bind it inside your button:
button1.disableProperty().bind(Bindings.isEmpty(textField1.textProperty()));
或者如果您想在多个文本字段为空时禁用按钮:
Or if you want to disable a button when multiple text fields are empty:
button1.disableProperty().bind(
Bindings.isEmpty(textField1.textProperty())
.or(Bindings.isEmpty(textField2.textProperty()))
.or(Bindings.isEmpty(textField3.textProperty()))
);
这篇关于如果TextField为空,如何禁用按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文