我只是在尝试 JavaFX,并且正在强行进入它,因为它被认为是 future 。我正在尝试创建一个包含 4 列的表。列和表应调整大小以填充父 Pane 。我一辈子都无法让它发挥作用。我已经尝试了 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。
查看我在 TableView 上添加的 GridPane.hgrow
和 GridPane.vgrow
约束:
<TableView fx:id="testTable"
GridPane.columnIndex="0"
GridPane.columnSpan="1"
GridPane.hgrow="ALWAYS"
GridPane.vgrow="ALWAYS"
GridPane.rowIndex="0">
FXML 仅反射(reflect) Java API,因此您也可以在 Java 源代码中执行相同操作;即GridPane.setHGrow(node, priority)
。不过,如果您使用 FXML 作为布局,建议在 FXML 中定义布局约束。
重构示例
我将您的 FXML 加载到 Scene Builder 2并设置适当的约束,当我使用场景生成器的预览功能时,它似乎可以自动调整大小。
<?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 中放置一个节点。只需将 TableView 直接放置在 BorderPane 的中心或使用更简单的布局父级(例如 StackPane)就可以了。但也许这只是更复杂的 UI 的一些简化版本,所以也许这就是您使用 GridPane 的原因。
进一步阅读
如果您有时间,请阅读 Node 中有关 JavaFX 布局的内容。 , Pane , Group和 GridPane javadoc 并尝试这个小layout bounds demo也是。
其他评论问题
mention a StackPane would work, but StackPanes don't seem to have Hgrow and Vgrow?
StackPanes 不需要 Hgrow 或 Vgrow 约束。这些限制设置Priorities ,定义为:
Enumeration used to determine the grow (or shrink) priority of a given node's layout area when its region has more (or less) space available and multiple nodes are competing for that space.
使用 StackPane 时,所有子节点都层叠在彼此之上,它们不会竞争空间,因此子节点的这种增长优先级设置不适用。
关于JavaFX 表格 View 调整大小以适合窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23687836/