英文:
How to get the row count of a TreeTableView?
问题
Edit: 我现在接受这个事实,方法 TreeTableView.getExpandedItemCount()
似乎实际上是在计算当前显示的所有 TreeItem
。这使得这个方法成为我碰巧发现的命名最糟糕的方法之一,原因在我最初的问题中已经阐述过,并在几个评论中重申过。
可以设计一个简单的树迭代器来计算 TreeTableView
(树表视图)的列0(树列)中的所有项目的数量(无论是显示还是因为一个或多个祖先被折叠而被隐藏)。
但是我找不到一种计算“显示”的行数的方法。我使用“显示”而不是“可见”,因为树行可以在没有必要可见的情况下显示。
我注意到有一个方法 TreeTableView.getExpandedItemCount()
。这并没有完全告诉您我想要知道的内容:您可以拥有一组叶子节点,每个叶子节点占据一行,但没有展开任何一个。同样,这些已展开的项可能包括一个或多个祖先实际上被折叠的 TreeItem
,仅仅因为一个 TreeItem
被展开并不意味着它被显示。
我还看了一下 TreeTableColumn
。我在那里没有看到什么非常明显的内容。
我唯一能想到的解决方法非常不优雅:
// 注意,TreeIterator 是一个简单的基于深度优先的堆栈迭代器
TreeIterator iterator = new TreeIterator(treeTableView.getRoot());
int nRows = 0;
while (iterator.hasNext()) {
TreeItem ti = iterator.next();
int row = treeTableView.getRow(ti);
if (row > nRows) nRows = row;
}
log.debug("显示的行数:" + nRows);
……肯定有比这更好的解决方法。
英文:
Edit: I now accept that the method TreeTableView.getExpandedItemCount()
in fact appears to count all the TreeItem
s currently displayed. This makes this method one of the most egregiously badly named methods I have ever had the misfortune to come across, for incontrovertible reasons stated in my original question, and reiterated in several comments.
A simple tree iterator can be devised to count all the items (regardless of whether showing, or concealed due to one or more ancestors being collapsed) in a TreeTableView
's column0 (the tree column).
But I can't find a way of counting the rows which are "displayed". I say "displayed" rather than "visible" because a tree row can be displayed without necessarily being visible.
I note that there is a method TreeTableView.getExpandedItemCount()
. This doesn't tell you quite what I want to know: you can have a set of leaves, each taking up one row, none of which is expanded. Equally it's possible that these expanded items might include TreeItem
s one or more of whose ancestors is in fact collapsed: just because a TreeItem
is expanded doesn't mean it is displayed.
I also had a look at TreeTableColumn
. I couldn't see anything very obvious there.
The only solution I can think of is supremely inelegant:
// NB TreeIterator is a simple depth-first stack-based iterator
TreeIterator iterator = new TreeIterator(treeTableView.getRoot());
int nRows = 0;
while (iterator.hasNext()) {
TreeItem ti = iterator.next();
int row = treeTableView.getRow( ti );
if( row > nRows ) nRows = row;
}
log.debug( "displayed rows " + nRows );
... there MUST, surely, be something better than that.
答案1
得分: 2
private static int getVisibleCellCount(TreeTableView<?> treeTableView) {
TreeTableViewSkin<?> skin = (TreeTableViewSkin<?>) treeTableView.getSkin();
Optional<VirtualFlow> vf = skin.getChildren().stream()
.filter(child -> child instanceof VirtualFlow)
.map(VirtualFlow.class::cast)
.findFirst();
if (vf.isPresent()) {
VirtualFlow<?> virtualFlow = vf.get();
IndexedCell first = virtualFlow.getFirstVisibleCell();
if (first != null) {
return virtualFlow.getCellCount();
}
}
return 0;
}
英文:
The method TreeTableView.getExpandedItemCount()
gives the number of rows displayed. Before it was realized that this simple solution was on offer, alternative solutions for getting the same thing were found as follows:
private static int getVisibleCellCount(TreeTableView<?> treeTableView) {
TreeTableViewSkin<?> skin = (TreeTableViewSkin<?>) treeTableView.getSkin();
Optional<VirtualFlow> vf = skin.getChildren().stream()
.filter(child -> child instanceof VirtualFlow)
.map(VirtualFlow.class::cast)
.findFirst();
if (vf.isPresent()) {
VirtualFlow<?> virtualFlow = vf.get();
IndexedCell first = virtualFlow.getFirstVisibleCell();
if (first != null) {
return virtualFlow.getCellCount();
}
}
return 0;
}
NOTE: Again, this is not a solution to the real problem. The solution only shows how to get the first cell and last cell in the viewport.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论