javafx 表格列拖拉_JavaFX技巧22:“自动调整大小(树)”表列
生活随笔
收集整理的這篇文章主要介紹了
javafx 表格列拖拉_JavaFX技巧22:“自动调整大小(树)”表列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
javafx 表格列拖拉
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
javafx 表格列拖拉
總結
以上是生活随笔為你收集整理的javafx 表格列拖拉_JavaFX技巧22:“自动调整大小(树)”表列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ddos是什么意思啊(DDOS是什么样的
- 下一篇: linux查看java进程命令(linu