JavaFX8:文本属性内容的 BooleanBinding disabledProperty 不起作用

JavaFX8 : BooleanBinding disabledProperty for content of textproperty not working

我正在尝试为节点的禁用 属性 创建布尔绑定。 如果 textProperty 值确实以定义的字符串结尾,则应启用节点:

myheckbox.disabledProperty().bind(Bindings.createBooleanBinding(()
                -> myLabel.getText().endsWith(".txt"), myLabel.textProperty()));

由于以下原因无法编译: ReadOnlyBooleanProperty

类型未定义方法 bind(BooleanBinding)

我该如何绑定它?该复选框最初是禁用的,如果标签包含以“.txt”结尾的文件名,则应启用该复选框

您试图绑定错误的 属性。有两个属性与节点的禁用状态相关:

  1. Node#disabled(只读)

    Indicates whether or not this Node is disabled. A Node will become disabled if disable is set to true on either itself or one of its ancestors in the scene graph.

  2. Node#disable(读写)

    Defines the individual disabled state of this Node. Setting disable to true will cause this Node and any subnodes to become disabled. This property should be used only to set the disabled state of a Node. For querying the disabled state of a Node, the disabled property should instead be used, since it is possible that a Node was disabled as a result of an ancestor being disabled even if the individual disable state on this Node is false.

你需要绑定后者属性。

myheckbox.disableProperty().bind(Bindings.createBooleanBinding(()
                -> myLabel.getText().endsWith(".txt"), myLabel.textProperty()));