Anchor button disables property for tree selection in fxml

I cannot find an example of simple binding in fxml only.

I want to enable / disable a button if the item is selected in tree view.

I tried this code (labels are used to test bindings):

<Scene xmlns:fx="http://javafx.com/fxml">
  <BorderPane>
    <top>
      <Button text="test" disable="${treeView.selectionModel.selectedItem.isNull}" />
    </top>
    <center>
      <TreeView fx:id="treeView" />
    </center>
    <bottom>
      <Label text="${treeView.selectionModel}" />
      <Label text="${treeView.selectionModel.selectedItem.isNull}" />
    </bottom>
  </BorderPane>
</Scene>

      

This code throws no errors.

The first label displays the treeView selection model toString()

.
The second label does not display anything, but should display true or false.
The button should turn on and off.

Any ideas?

+3


source to share


1 answer


From the code, you can do something like this:



treeview.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>()
    {
        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
        {
            if (newValue.intValue() > 0)
                button.setDisable(true);
            else
                button.setDisable(false);
        }
});

      

0


source







All Articles