如何在 FXML 中给 TableColumns 一个相对宽度?

How to give TableColumns a relative width in FXML?

是否有可能在 FXML 中获得调整大小的 TableView,其中每列相对调整自身大小? 我知道这在代码中是可能的,但我正在专门寻找 FXML 方法。

这是当前的方法:

<BorderPane xmlns:fx="http://javafx.com/fxml">
    <fx:define>
        <Double fx:id="tableViewWidth" fx:value="600"/>
    </fx:define>
    <center>
        <TableView fx:id="expensesTableView" editable="true" prefWidth="${tableViewWidth}">
            <columnResizePolicy>
                <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
            </columnResizePolicy>
            <columns>
                <TableColumn text="Title" prefWidth="${tableViewWidth * 3}">
                    <cellValueFactory>
                        <PropertyValueFactory property="title" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Category" prefWidth="${tableViewWidth * 3}">
                    <cellValueFactory>
                        <PropertyValueFactory property="category" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Period" prefWidth="${tableViewWidth * 2}">
                    <cellValueFactory>
                        <PropertyValueFactory property="period" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Value" prefWidth="${tableViewWidth * 2}">
                    <cellValueFactory>
                        <PropertyValueFactory property="value" />
                    </cellValueFactory>
                </TableColumn>
            </columns>
        </TableView>
    </center>
</BorderPane>

乘法部分似乎不起作用。

您需要删除调整大小策略并绑定到 TableView 的宽度而不是某个双精度常量。

此外,一些用于绑定的表达式在语法上是错误的...

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.cell.*?>

<BorderPane xmlns:fx="http://javafx.com/fxml">
    <center>
        <TableView fx:id="expensesTableView" editable="true" prefWidth="600">
            <columns>
                <TableColumn text="Title" prefWidth="${expensesTableView.width*0.3}">
                    <cellValueFactory>
                        <PropertyValueFactory property="title" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Category" prefWidth="${expensesTableView.width*0.3}">
                    <cellValueFactory>
                        <PropertyValueFactory property="category" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Period" prefWidth="${expensesTableView.width*0.2}">
                    <cellValueFactory>
                        <PropertyValueFactory property="period" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Value" prefWidth="${expensesTableView.width*0.2}">
                    <cellValueFactory>
                        <PropertyValueFactory property="value" />
                    </cellValueFactory>
                </TableColumn>
            </columns>
        </TableView>
    </center>
</BorderPane>