使用FXML在用户控件中创建并绑定DoubleProperty

时间:2018-10-11 07:41:02

标签: javafx binding user-controls fxml

我创建一个代表NumPad键盘的用户控件。我希望将各个按钮的宽度和高度设置为/resources,以便稍后进行配置。

我遇到的问题是我无法将属性绑定到DoubleProperty文件中prefWidth列和行的prefHeightGridPane。我设法做到这一点的唯一方法是在控制控制器中建立连接。但是我想避免这种情况。

这是有问题的FXML文件。

FXML

<fx:root type="javafx.scene.layout.GridPane" xmlns="http://javafx.com/javafx/" xmlns:fx="http://javafx.com/fxml/1"> <fx:define> <SimpleDoubleProperty fx:id="size"> <value> <Double fx:value="50"/> </value> </SimpleDoubleProperty> </fx:define> <gridLinesVisible>false</gridLinesVisible> <hgap>5</hgap> <vgap>5</vgap> <Button text="C" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="0" focusTraversable="false"/> <Button text="Backspace" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="0" GridPane.columnSpan="2" focusTraversable="false"/> <Button text="7" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="1" focusTraversable="false"/> <Button text="8" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="1" GridPane.rowIndex="1" focusTraversable="false"/> <Button text="9" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="1" focusTraversable="false"/> <Button text="4" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="2" focusTraversable="false"/> <Button text="5" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="1" GridPane.rowIndex="2" focusTraversable="false"/> <Button text="6" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="2" focusTraversable="false"/> <Button text="1" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="3" focusTraversable="false"/> <Button text="2" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="1" GridPane.rowIndex="3" focusTraversable="false"/> <Button text="3" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="3" focusTraversable="false"/> <Button text="0" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="4" GridPane.columnSpan="2" focusTraversable="false"/> <Button text="." maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="2" GridPane.rowIndex="4" focusTraversable="false"/> <Button text="E" onAction="#f" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnIndex="3" GridPane.rowIndex="2" GridPane.rowSpan="3" focusTraversable="false"/> <columnConstraints> <ColumnConstraints fx:id="col1" prefWidth="${size.value}"/> <ColumnConstraints fx:id="col2" prefWidth="${size.value}"/> <ColumnConstraints fx:id="col3" prefWidth="${size.value}"/> <ColumnConstraints fx:id="col4" prefWidth="${size.value}"/> </columnConstraints> <rowConstraints> <RowConstraints fx:id="row1" prefHeight="${size.value}"/> <RowConstraints fx:id="row2" prefHeight="${size.value}"/> <RowConstraints fx:id="row3" prefHeight="${size.value}"/> <RowConstraints fx:id="row4" prefHeight="${size.value}"/> <RowConstraints fx:id="row5" prefHeight="${size.value}"/> </rowConstraints> </fx:root> 正确地初始化了控件,但是${size.value}size / ColumnConstraints#prefWidthProperty之间没有绑定。

如果我手动进行绑定,则一切正常。但是正如我所说,如果可能的话,我想避免它。

RowConstraints#prefHeightProperty

如果我使用@FXML private void initialize() { col1.prefWidthProperty().bind(size); // ... row1.prefHeightProperty().bind(size); // ... } ,则会得到NumberFormatException:对于输入字符串:“ DoubleProperty [value:50.0]”

1 个答案:

答案 0 :(得分:3)

通过表达式绑定似乎无法直接访问属性。

但是,如果您通过控制器提供对属性的访问,则可以使用${controller.size}来创建绑定:

@FXML
private DoubleProperty size;

public final double getSize() {
    return size.get();
}

public final void setSize(double value) {
    size.set(value);
}

public final DoubleProperty sizeProperty() {
    return size;
}
...
<columnConstraints>
    <ColumnConstraints fx:id="col1" prefWidth="${controller.size}"/>
    <ColumnConstraints fx:id="col2" prefWidth="${controller.size}"/>
    <ColumnConstraints fx:id="col3" prefWidth="${controller.size}"/>
    <ColumnConstraints fx:id="col4" prefWidth="${controller.size}"/>
</columnConstraints>
<rowConstraints>
    <RowConstraints fx:id="row1" prefHeight="${controller.size}"/>
    <RowConstraints fx:id="row2" prefHeight="${controller.size}"/>
    <RowConstraints fx:id="row3" prefHeight="${controller.size}"/>
    <RowConstraints fx:id="row4" prefHeight="${controller.size}"/>
    <RowConstraints fx:id="row5" prefHeight="${controller.size}"/>
</rowConstraints>
...