如何在TableML中给TableColumns一个相对宽度? [英] How to give TableColumns a relative width in FXML?

查看:133
本文介绍了如何在TableML中给TableColumns一个相对宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在FXML中调整TableView的大小,其中每列的相对大小调整?
我知道它可以在代码中使用,但我特意寻找FXML方法。

Is it possible to get a resizing TableView in FXML, where each columns resizes itself relatively? I know it's possible in code, but I'm specifically looking for an FXML approach.

这是当前的方法:

<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>

乘法部分似乎不起作用。

The multiply-part doesn't seem to work.

推荐答案

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

You need to remove the resize policy and also bind to the width of the TableView instead to some double constant.

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

Furthermore some of the expressions used for the binding are syntactically wrong...

<?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>

这篇关于如何在TableML中给TableColumns一个相对宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆