【问题标题】:TableView tableColumn width is not considering header when autosizing javafx自动调整 javafx 大小时,TableView tableColumn 宽度不考虑标题
【发布时间】:2020-10-11 09:07:17
【问题描述】:

需要帮助解决表格视图 JavaFX 上的显示问题。

我无法粘贴完整的代码。但是,将尝试包含最大值。

TextField headerTextField = new TextField();
Label label = new Label((String) allColumns[i]);
VBox headerGraphic = new VBox();
headerGraphic.setAlignment(Pos.CENTER);
headerGraphic.getChildren().addAll(label, headerTextField);
TableColumn tableColumn = new TableColumn<>();
tableColumn.setGraphic(headerGraphic);

输出是:

如果我不设置图形,直接用列名创建表格列,看起来不错。

TableColumn tableColumn = new TableColumn<>((String) allColumns[i]);

输出是:

更新: 我通过使用 Text 而不是 Label 解决了这个问题。似乎只有在加载场景后才计算标签的宽度。因此,未设置表格列首选项宽度。

使用下面的代码,它工作了。

TextField headerTextField = new TextField();
Text label = new Text((String) allColumns[i]);
VBox headerGraphic = new VBox();
headerGraphic.setAlignment(Pos.CENTER);
headerGraphic.getChildren().addAll(label, headerTextField);
TableColumn tableColumn = new TableColumn<>();
tableColumn.setGraphic(headerGraphic);

输出是:

【问题讨论】:

  • 看起来像一个错误 - 它是哪个 fx 版本?
  • @kleopatra,它是 javafx 14.0.1
  • 是的,绝对是一个bug,可以重现(隐约记得前段时间见过):初始大小不正确,后来双击调整区域大小就可以了。在将表格添加到场景后,您可以修改并手动为每个标题调用 resizeColumnToFitContent。通过变脏并反射性地访问该方法,为每个标题实现一个自定义 columnHeader,该方法公开该方法(以及整个堆栈 TableViewSkin、TableHeaderRow、NestedTableColumnHeader,其唯一目的是使用该自定义标题;)
  • @klepatra,谢谢。我通过用文本替换标签来修复它。似乎标签的宽度仅在布局后计算。不必破解调整大小的方法。用结果编辑了上面的帖子。 :)
  • good :) 您可以考虑自行回答(并接受答案)您的问题,以便未来的读者可以找到它。

标签: javafx tableview width tablecolumn


【解决方案1】:

我通过使用文本而不是标签来解决它。似乎只有在加载场景后才计算标签的宽度。因此,未设置表格列首选项宽度。编辑了上面的帖子。

更新: 我不得不使用调整大小自定义方法,因为当表没有记录时,上述方法不起作用。 所以,我调用了下面的函数,它适用于有记录的表和没有记录的表。

public static void autoResizeColumns( TableView<?> table )
    {
        //Set the right policy
        table.getColumns().stream().forEach( (column) ->
        {
            Text t = new Text( column.getText() );
            double max = 0.0f;
            if("".equals(t.getText()))
            {
                VBox vBox = (VBox) column.getGraphic();
                ObservableList<Node> vBoxChild = vBox.getChildren();
                max = vBoxChild.get(0).getLayoutBounds().getWidth();
            }
            else
            {
                max = t.getLayoutBounds().getWidth();
            }
            
            for ( int i = 0; i < table.getItems().size(); i++ )
            {
                //cell must not be empty
                if ( column.getCellData( i ) != null )
                {
                    t = new Text( column.getCellData( i ).toString() );
                    double calcwidth = t.getLayoutBounds().getWidth();
                    //remember new max-width
                    if ( calcwidth > max )
                    {
                        max = calcwidth;
                    }
                }
            }
            //set the new max-widht with some extra space
            column.setPrefWidth( max + 15.0d );
        } );
    }

【讨论】:

    猜你喜欢
    • 2020-05-07
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2014-10-31
    相关资源
    最近更新 更多