JavaFX - 将TableView高度调整为行数 [英] JavaFX - Adapt TableView height to number of rows

查看:1203
本文介绍了JavaFX - 将TableView高度调整为行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的TableView的高度适应填充的行数,因此它永远不会显示任何空行。换句话说,TableView的高度不应超出最后填充的行。我该怎么办?

I want my TableView's height to adapt to the number of filled rows, so that it never shows any empty rows. In other words, the height of the TableView should not go beyond the last filled row. How do I do this?

推荐答案

如果你希望这个工作,你必须设置 fixedCellSize

If you want this to work you have to set the fixedCellSize.

然后你可以将 TableView 的高度绑定到包含在表乘以固定的单元格大小。

Then you can bind the height of the TableView to the size of the items contained in the table multiplied by the fixed cell size.

演示:

@Override
public void start(Stage primaryStage) {

    TableView<String> tableView = new TableView<>();
    TableColumn<String, String> col1 = new TableColumn<>();
    col1.setCellValueFactory(cb -> new SimpleStringProperty(cb.getValue()));
    tableView.getColumns().add(col1);
    IntStream.range(0, 20).mapToObj(Integer::toString).forEach(tableView.getItems()::add);

    tableView.setFixedCellSize(25);
    tableView.prefHeightProperty().bind(tableView.fixedCellSizeProperty().multiply(Bindings.size(tableView.getItems()).add(1.01)));
    tableView.minHeightProperty().bind(tableView.prefHeightProperty());
    tableView.maxHeightProperty().bind(tableView.prefHeightProperty());

    BorderPane root = new BorderPane(tableView);
    root.setPadding(new Insets(10));
    Scene scene = new Scene(root, 400, 400);
    primaryStage.setScene(scene);
    primaryStage.show();
}

注意:我乘以fixedCellSize *(数据大小+ 1.01)以包含标题行。

Note: I multiplied fixedCellSize * (data size + 1.01) to include the header row.

这篇关于JavaFX - 将TableView高度调整为行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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