如何使 JavaFX TableView 中的最后一列占用剩余空间。
我已经尝试过
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
但这使得列大小相等。当窗口宽度增加时,我只希望最后一列增长。
如果您希望表视图中的所有列填满表视图可用的窗口空间并动态调整大小以适应窗口大小的变化,那么您需要将列属性绑定到表视图的宽度。
例如,假设我的表格视图中有 5 列。我希望它们始终是可用宽度的固定百分比(这样当调整窗口大小时,列的比例保持不变)。您可以使用相同的属性绑定习惯轻松形成自己的列大小调整规则(例如,我希望最后一列占据所有剩余空间)。
在具有 TableView 控件的控制器的初始化()方法中,我可以执行以下操作:
void initialize() {
// Initialize your logic here: all @FXML variables will have been injected
:
:
// TableView column control variables are prefixed with "tco"
tcoLast.setCellValueFactory(new PropertyValueFactory<Client.Expand, String>("lastName"));
tcoFirst.setCellValueFactory(new PropertyValueFactory<Client.Expand, String>("firstName"));
tcoDoB.setCellValueFactory(new PropertyValueFactory<Client.Expand, Integer>("doB"));
tcoMRN.setCellValueFactory(new PropertyValueFactory<Client.Expand, String>("defMRN"));
tcoGen.setCellValueFactory(new PropertyValueFactory<Client.Expand, String>("gender"));
// Cell factories for rendering certain columns in the TableView
tcoLast.setCellFactory(new ClientNameTableCellFactory());
tcoFirst.setCellFactory(new ClientNameTableCellFactory());
tcoDoB.setCellFactory(new ClientDoBTableCellFactory());
// Set fixed column widths that resize automatically
// Values are weighted to be a fraction of a total of 41 (arbitrary)
tcoLast.prefWidthProperty().bind(tbvMatches.widthProperty().multiply(11.0/41.0));
tcoFirst.prefWidthProperty().bind(tbvMatches.widthProperty().multiply(11.0/41.0));
tcoDoB.prefWidthProperty().bind(tbvMatches.widthProperty().multiply(8.0/41.0));
tcoMRN.prefWidthProperty().bind(tbvMatches.widthProperty().multiply(8.0/41.0));
tcoGen.prefWidthProperty().bind(tbvMatches.widthProperty().multiply(2.0/41.0));
:
:
}
这是我基于 FXML 的解决方案:
<TableView fx:id="log">
<columns>
<TableColumn fx:id="logDir" maxWidth="-1.0" minWidth="50.0" prefWidth="-1.0" text="Dir" />
<TableColumn fx:id="logTime" maxWidth="-1.0" minWidth="100.0" prefWidth="-1.0" text="Time" />
<TableColumn fx:id="logHex" maxWidth="-1.0" minWidth="100.0" prefWidth="-1.0" text="Hex" />
<TableColumn fx:id="logDescription" maxWidth="1.7976931348623157E308" minWidth="100.0"
prefWidth="${log.width-logDir.width-logTime.Width-logHex.width}" sortable="false" text="Description" />
</columns>
</TableView>