JavaFX tableview调整大小以适合窗口
我正在尝试
JavaFX,并强迫我的方式,因为它是假设是未来.我正在尝试创建一个4列的表.列AND TABLE应调整大小以填充父窗格.我不能为我的生活让这工作.我一直在尝试超过4个小时,而且tableview没有调整大小.
这是我的FXML文件. <?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import java.net.*?> <?import javafx.geometry.*?> <?import javafx.geometry.Insets?> <?import javafx.scene.control.*?> <?import javafx.scene.control.cell.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> <?import javafx.collections.*?> <?import t.cubed.fxml.*?> <BorderPane styleClass="root" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="t.cubed.fxml.FXMLDocumentController"> <top> <MenuBar fx:id="menuBar" styleClass="menu-bar"> <menus> <Menu text="File"> <items> <MenuItem onAction="#handleOpenAction" text="Open" /> <MenuItem onAction="#handleExitAction" text="Exit" /> </items> </Menu> <Menu text="Edit"> <items> <MenuItem onAction="#handleWeightAction" text="Edit Weights" /> <MenuItem onAction="#handleFilterAction" text="Edit Filters" /> <MenuItem onAction="#handleOptionsAction" text="Options" /> </items> </Menu> </menus> </MenuBar> </top> <center> <GridPane> <!-- <padding> <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/> </padding> --> <TableView fx:id="testTable" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="1"> <columnResizePolicy> <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /> </columnResizePolicy> <columns> <TableColumn text="TEST NUMBER"> <cellValueFactory> <PropertyValueFactory property="testNumber" /> </cellValueFactory> </TableColumn> <TableColumn text="TEST NAME"> <cellValueFactory> <PropertyValueFactory property="testName" /> </cellValueFactory> </TableColumn> <TableColumn text="TEST TIME(ms)"> <cellValueFactory> <PropertyValueFactory property="testTime" /> </cellValueFactory> </TableColumn> <TableColumn text="BEST MATCH"> <cellValueFactory> <PropertyValueFactory property="bestMatch" /> </cellValueFactory> </TableColumn> </columns> <items> <!-- <FXCollections fx:factory="observableArrayList"> <TestTableModel testNumber="100" testName="Test1234" testTime="0.34" bestMatch="99"/> </FXCollections> --> </items> </TableView> </GridPane> </center> <stylesheets> <URL value="@t-cubed.css" /> </stylesheets> </BorderPane> 解决方法
可调整大小的布局如何工作
JavaFX中可调整大小的项目的大小由放置项目的布局管理器进行管理. 你需要做什么 对于您的特定问题,您需要设置GridPane大小限制来管理您放置在网格中的TableView. 看看GridPane.hgrow和GridPane.vgrow约束我添加在TableView上: <TableView fx:id="testTable" GridPane.columnIndex="0" GridPane.columnSpan="1" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS" GridPane.rowIndex="0"> FXML只是反映了Java API,所以你也可以在Java源中做同样的事情,即 重构样品 我在Scene Builder 2中加载了FXML,并设置了相应的约束,当我使用Scene Builder的预览功能时,它似乎自动调整大小. <?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import java.net.*?> <?import javafx.geometry.*?> <?import javafx.geometry.Insets?> <?import javafx.scene.control.*?> <?import javafx.scene.control.cell.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> <?import javafx.collections.*?> <?import t.cubed.fxml.*?> <BorderPane styleClass="root" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="t.cubed.fxml.FXMLDocumentController"> <top> <MenuBar fx:id="menuBar" styleClass="menu-bar"> <menus> <Menu text="File"> <items> <MenuItem onAction="#handleOpenAction" text="Open" /> <MenuItem onAction="#handleExitAction" text="Exit" /> </items> </Menu> <Menu text="Edit"> <items> <MenuItem onAction="#handleWeightAction" text="Edit Weights" /> <MenuItem onAction="#handleFilterAction" text="Edit Filters" /> <MenuItem onAction="#handleOptionsAction" text="Options" /> </items> </Menu> </menus> </MenuBar> </top> <center> <GridPane> <children> <!-- <padding> <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/> </padding> --> <TableView fx:id="testTable" GridPane.columnIndex="0" GridPane.columnSpan="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="0" GridPane.vgrow="ALWAYS"> <columnResizePolicy> <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /> </columnResizePolicy> <columns> <TableColumn text="TEST NUMBER"> <cellValueFactory> <PropertyValueFactory property="testNumber" /> </cellValueFactory> </TableColumn> <TableColumn text="TEST NAME"> <cellValueFactory> <PropertyValueFactory property="testName" /> </cellValueFactory> </TableColumn> <TableColumn text="TEST TIME(ms)"> <cellValueFactory> <PropertyValueFactory property="testTime" /> </cellValueFactory> </TableColumn> <TableColumn text="BEST MATCH"> <cellValueFactory> <PropertyValueFactory property="bestMatch" /> </cellValueFactory> </TableColumn> </columns> <items> <!-- <FXCollections fx:factory="observableArrayList"> <TestTableModel testNumber="100" testName="Test1234" testTime="0.34" bestMatch="99"/> </FXCollections> --> </items> </TableView> </children> <columnConstraints> <ColumnConstraints /> </columnConstraints> <rowConstraints> <RowConstraints /> </rowConstraints> </GridPane> </center> <stylesheets> <URL value="@t-cubed.css" /> </stylesheets> </BorderPane> 一些忠告 您将TableView放置在GridPane中.在您的情况下使用GridPane有点奇怪,因为GridPane只会在GridPane中放置一个节点.只需将TableView直接放置在BorderPane的中心,或者使用一个更简单的布局父类(如StackPane)就可以正常工作.但也许这只是一个更复杂的UI的一些削减版本,所以也许这就是为什么你使用了一个GridPane. 进一步阅读 如果您有时间,请阅读Node,Pane,Group和GridPane javadoc中的JavaFX中的布局,并尝试这个小小的layout bounds demo. 评论的其他问题
StackPanes不需要管道或Vgrow约束.这些约束设定为Priorities,定义如下:
使用StackPane,所有孩子都是彼此分层的,它们不会竞争空间,因此子节点的增长优先级设置不适用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |