陇南市建设局网站公示,做pc端网站教程,网站制作镇江,微网站微商城JavaFX “缺少功能调查”中提到的“缺少功能”的第一件事就是能够自动调整表/树表中的列大小。 没错#xff0c;没有公共API是正确的#xff0c;但是当您密切关注时#xff0c;您会注意到JavaFX内部一定有执行此操作的代码#xff0c;因为用户可以通过双击分隔线自动调整列… 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 (TreeTableColumnR, ? 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(TreeTableColumnR, ? 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(TreeTableColumnR, ? tc, int maxRows) {final TreeTableColumn col tc;List? items getItems();if (items null || items.isEmpty()) {return;}Callback cellFactory tc.getCellFactory();if (cellFactory null) {return;}TreeTableCellR, ? cell (TreeTableCellR, ?) 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();}TreeTableRowR 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