javafx-8groovyfx

Bind number property to boolean property


I'm using a TitledPane inside of GridPane and I want to set the percentHeight on the grid panes row constraints depending on whether or not the TitledPane is expanded. e.g.

gridPane {
    columnConstraints(hgrow: 'always')
    rowConstraints(vgrow: 'always')  // first row
    wgridRow1 = rowConstraints(percentHeight: 30) //2nd row
    node(column:0, row:0) // 
    tp = titledPane(column: 0, row: 1) {
        listView(items: ['one', 'two', 'three'])
    }
}

Can I use bindings to set the row constraints percentHeight based on whether the titled pane is expanded or not? e.g.

wgridRow1.percentHeightProperty().bind(tp.expandedProperty()).using
{ it ? 30 : -1 }

I can solve by adding a listener to the expanded property but I just wondered if it could be done through uni-directional binding.

Thanks,

Paul


Solution

  • You can use the Bindings API. I don't know the Groovy syntax, but in JavaFX it would look like

    wgridRow1.percentHeightProperty().bind(Bindings
        .when(tp.expandedProperty())
        .then(30)
        .otherwise(-1));