【发布时间】:2015-05-17 15:28:39
【问题描述】:
如何让 JavaFX TableView 中的最后一列占用剩余空间。
我试过table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 但这使得列大小相等。当窗口宽度增加时,我只希望最后一列增加。
【问题讨论】:
-
你可以为之前的tableColumns设置MaxWidth。
如何让 JavaFX TableView 中的最后一列占用剩余空间。
我试过table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 但这使得列大小相等。当窗口宽度增加时,我只希望最后一列增加。
【问题讨论】:
如果您希望 tableview 中的所有列都填满 tableview 可用的窗口空间并动态调整大小以适应更改窗口的大小,那么您需要将列属性绑定到 tableview 的宽度。
例如,假设我在一个 tableview 中有 5 列。我希望它们始终是可用宽度的固定百分比(以便在调整窗口大小时,列的比例保持不变)。您可以使用相同的属性绑定习惯轻松形成自己的列大小调整规则(例如,我希望最后一列占据所有剩余空间)。
在具有我的 TableView 控件的控制器的 initialize() 方法中,我可以执行以下操作:
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));
:
:
}
【讨论】: