JavaFX绑定到多个属性 [英] JavaFX bind to multiple properties

查看:412
本文介绍了JavaFX绑定到多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带文本字段和按钮的简单fxml。如果textfield为空,我想禁用该按钮。所以我在我的控制器中插入如下内容:

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 controller:

@Override
public void initialize(URL url, ResourceBundle bundle) {
  button.disableProperty().bind(textField.textProperty().isEqualTo(""));
}

..并且工作正常。问题是当我添加第二个文本字段并希望如果任一文本字段为空时我的按钮被禁用。该怎么办?我尝试了以下方法,但这不起作用:

..and that works fine. The problem is when I add a second textfield and would like my button to be disabled if either textfield is empty. What to do then? I tried the following, but that doesn't work:

@Override
public void initialize(URL url, ResourceBundle bundle) {
  button.disableProperty().bind(textField.textProperty().isEqualTo(""));
  button.disableProperty().bind(textField2.textProperty().isEqualTo(""));
}


推荐答案

这可以通过绑定到布尔表达式通过 Bindings

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

button.disableProperty().bind(
    Bindings.and(
        textField.textProperty().isEqualTo(""),
        textField2.textProperty().isEqualTo("")));

这篇关于JavaFX绑定到多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆