gpt4 book ai didi

JavaFX TableColumn调整大小以适合单元格内容

转载 作者:行者123 更新时间:2023-12-04 03:27:26 24 4
gpt4 key购买 nike

我正在寻找一种在TableView中调整TableColumn大小的方法,以便所有内容在每个单元格中都是可见的(即无截断)。

我注意到双击列分隔符的确使该列自动适合其单元格的内容。有没有办法以编程方式触发此操作?

最佳答案

深入研究javafx源,我发现单击TableView列分隔符时调用的实际方法是

/*
* FIXME: Naive implementation ahead
* Attempts to resize column based on the pref width of all items contained
* in this column. This can be potentially very expensive if the number of
* rows is large.
*/
@Override protected void resizeColumnToFitContent(TableColumn<T, ?> tc, int maxRows) {
if (!tc.isResizable()) return;

// final TableColumn<T, ?> col = tc;
List<?> items = itemsProperty().get();
if (items == null || items.isEmpty()) return;

Callback/*<TableColumn<T, ?>, TableCell<T,?>>*/ cellFactory = tc.getCellFactory();
if (cellFactory == null) return;

TableCell<T,?> cell = (TableCell<T, ?>) 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 TableColumnBase
cell.getProperties().put(TableCellSkin.DEFER_TO_PARENT_PREF_WIDTH, Boolean.TRUE);

// determine cell padding
double padding = 10;
Node n = cell.getSkin() == null ? null : cell.getSkin().getNode();
if (n instanceof Region) {
Region r = (Region) n;
padding = r.snappedLeftInset() + r.snappedRightInset();
}

int rows = maxRows == -1 ? items.size() : Math.min(items.size(), maxRows);
double maxWidth = 0;
for (int row = 0; row < rows; row++) {
cell.updateTableColumn(tc);
cell.updateTableView(tableView);
cell.updateIndex(row);

if ((cell.getText() != null && !cell.getText().isEmpty()) || cell.getGraphic() != null) {
getChildren().add(cell);
cell.applyCss();
maxWidth = Math.max(maxWidth, cell.prefWidth(-1));
getChildren().remove(cell);
}
}

// dispose of the cell to prevent it retaining listeners (see RT-31015)
cell.updateIndex(-1);

// RT-36855 - take into account the column header text / graphic widths.
// Magic 10 is to allow for sort arrow to appear without text truncation.
TableColumnHeader header = getTableHeaderRow().getColumnHeaderFor(tc);
double headerTextWidth = Utils.computeTextWidth(header.label.getFont(), tc.getText(), -1);
Node graphic = header.label.getGraphic();
double headerGraphicWidth = graphic == null ? 0 : graphic.prefWidth(-1) + header.label.getGraphicTextGap();
double headerWidth = headerTextWidth + headerGraphicWidth + 10 + header.snappedLeftInset() + header.snappedRightInset();
maxWidth = Math.max(maxWidth, headerWidth);

// RT-23486
maxWidth += padding;
if(tableView.getColumnResizePolicy() == TableView.CONSTRAINED_RESIZE_POLICY) {
maxWidth = Math.max(maxWidth, tc.getWidth());
}

tc.impl_setWidth(maxWidth);
}

声明在
com.sun.javafx.scene.control.skin.TableViewSkinBase

方法签名
protected abstract void resizeColumnToFitContent(TC tc, int maxRows)

由于它是 protected ,因此您无法通过例如tableView.getSkin(),但是您始终可以扩展TableViewSkin,使其仅覆盖resizeColumnToFitContent方法并将其公开。

关于JavaFX TableColumn调整大小以适合单元格内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23284437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com