JavaFX “缺少功能调查”中提到的“缺少功能”的第一件事就是能够自动调整表/树表中的列大小。 没错,没有公共API是正确的,但是当您密切关注时,您会注意到JavaFX内部一定有执行此操作的代码,因为用户可以通过双击分隔线自动调整列的大小。在该列与右侧的下一列之间。
但是像大多数人一样,我觉得这对我的代码还不够好。 我想要一个FlexGanttFX的API,该API允许用户自动调整甘特图内一列或所有列的大小。 因此,我搜索了隐藏在树表或树表皮肤某处(实际上不记得在哪里)的代码,并在类中进行了一些细微修改以重新使用它。
以下是这项工作的结果。 它以TreeTableView而不是TableView为目标,但是使它适用于标准表很简单。 只需将所有TreeTableColumn出现替换为TableColumn即可 。 请注意,调整所有行的大小可能会对性能产生严重影响,因此您可能必须通过maxRows参数来限制要用于计算的行数 。
/*** This method will resize all columns in the tree table view to ensure that* the content of all cells will be completely visible. Note: this is a very* expensive operation and should only be used when the number of rows is* small.** @see #resizeColumn(TreeTableColumn, int)*/public final void resizeColumns() {resizeColumns(-1);}/*** This method will resize all columns in the tree table view to ensure that* the content of all cells will be completely visible. Note: this is a very* expensive operation and should only be used with a small number of rows.** @param maxRows* the maximum number of rows that will be considered for the* width calculations** @see #resizeColumn(TreeTableColumn, int)*/public final void resizeColumns(int maxRows) {for (TreeTableColumn<R, ?> column : getTreeTable().getColumns()) {resizeColumn(column, maxRows);}}/*** This method will resize the given column in the tree table view to ensure* that the content of the column cells will be completely visible. Note:* this is a very expensive operation and should only be used when the* number of rows is small.** @see #resizeColumn(TreeTableColumn, int)*/public final void resizeColumn(TreeTableColumn<R, ?> column) {resizeColumn(column, -1);}/*** This method will resize the given column in the tree table view to ensure* that the content of the column cells will be completely visible. Note:* this is a very expensive operation and should only be used when the* number of rows is small.** @see #resizeColumn(TreeTableColumn, int)*/public final void resizeColumn(TreeTableColumn<R, ?> tc, int maxRows) {final TreeTableColumn col = tc;List<?> items = getItems();if (items == null || items.isEmpty()) {return;}Callback cellFactory = tc.getCellFactory();if (cellFactory == null) {return;}TreeTableCell<R, ?> cell = (TreeTableCell<R, ?>) cellFactory.call(tc);if (cell == null) {return;}// set this property to tell the TableCell we want to know its actual// preferred width, not the width of the associated TableColumnBasecell.getProperties().put("deferToParentPrefWidth", Boolean.TRUE); //$NON-NLS-1$// determine cell paddingdouble padding = 10;Node n = cell.getSkin() == null ? null : cell.getSkin().getNode();if (n instanceof Region) {Region r = (Region) n;padding = r.snappedLeftInset() + r.snappedRightInset();}TreeTableRow<R> treeTableRow = new TreeTableRow<>();treeTableRow.updateTreeTableView(treeTableView);int rows = maxRows == -1 ? items.size(): Math.min(items.size(), maxRows);double maxWidth = 0;for (int row = 0; row < rows; row++) {treeTableRow.updateIndex(row);treeTableRow.updateTreeItem(treeTableView.getTreeItem(row));cell.updateTreeTableColumn(col);cell.updateTreeTableView(treeTableView);cell.updateTreeTableRow(treeTableRow);cell.updateIndex(row);if ((cell.getText() != null && !cell.getText().isEmpty())|| cell.getGraphic() != null) {getChildren().add(cell);cell.impl_processCSS(false);double w = cell.prefWidth(-1);maxWidth = Math.max(maxWidth, w);getChildren().remove(cell);}}// dispose of the cell to prevent it retaining listeners (see RT-31015)cell.updateIndex(-1);// RT-23486double widthMax = maxWidth + padding;if (treeTableView.getColumnResizePolicy() == TreeTableView.CONSTRAINED_RESIZE_POLICY) {widthMax = Math.max(widthMax, tc.getWidth());}tc.impl_setWidth(widthMax);}
翻译自: https://www.javacodegeeks.com/2015/12/javafx-tip-22-autosize-tree-table-columns.html