-3

Edit: I now accept that the method TreeTableView.getExpandedItemCount() in fact appears to count all the TreeItems 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 TreeItems 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.

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • How about `getroot().getChildren().size()` – c0der Apr 05 '20 at 17:09
  • What Java version are you using? – Miss Chanandler Bong Apr 05 '20 at 17:15
  • @c0der Please go and look up trees. – mike rodent Apr 05 '20 at 17:17
  • @M.S. Why do you ask? 11.0.2. – mike rodent Apr 05 '20 at 17:17
  • 1
    @mikerodent I'm asking because I'm trying to help – Miss Chanandler Bong Apr 05 '20 at 17:18
  • 1
    mike rodent As @c0der said, please check the value of getRoot().getChildren().size(). A TreeTableView is a subclass of other JavaFX objects, and some methods are inherited. – NomadMaker Apr 05 '20 at 17:24
  • @NomadMaker you also need to go and look up trees – mike rodent Apr 05 '20 at 17:27
  • @NomadMaker you definitely need to go and look up trees. – mike rodent Apr 05 '20 at 17:30
  • Compare your proposed solution to `treeTableView.getRoot().getChildren().size()` – c0der Apr 05 '20 at 17:32
  • Why would I need to look up trees, and which specific object are you ordering us to look up? I've been developing in Java since the first public beta. While I don't know everything, I do know how to look them up. – NomadMaker Apr 05 '20 at 17:33
  • `VirtualFlow` provides `getFirstVisibleCell()`, `getLastVisibleCell()`, `getLastVisibleCellWithinViewPort()` and `getFirstVisibleCellWithinViewPort()` methods. – Miss Chanandler Bong Apr 05 '20 at 17:36
  • Have you tested this? – NomadMaker Apr 05 '20 at 17:36
  • @M.S. Please could you post an answer? How are we to get hold of such a `VirtualFlow` from the `TreeTableView`? – mike rodent Apr 05 '20 at 17:43
  • 1
    I am not a down voter but I must say I find you responses unpleasant. – c0der Apr 05 '20 at 17:50
  • a leaf cannot be expanded (or always is) - and expandedItemCount seems what you want ... except you didn't explain it well enough or I'm too tired to understand it – kleopatra Apr 05 '20 at 21:45
  • expandedItemCount _is essentially the count of all expanded tree items, and their children._ don't understand how that can possibly be misunderstood .. – kleopatra Apr 05 '20 at 21:56
  • @kleopatra I now accept that that is what that method (astonishingly) delivers. But this makes it a shockingly badly named method. The name should be `getDisplayedItems()`. I repeat (you of all people with your compendious knowledge of all things Java GUI must know this): if you expand `TreeItem` X and then collapse one of its ancestors, in event Y, `TreeItem` X remains expanded, although it and all the TreeItems displayed under it prior to collapse event Y are now **non-displayed**. Expansion involves display but an "expanded items" count is VERY different to a "displayed items" count. – mike rodent Apr 05 '20 at 23:50
  • Names are often open to debate, but don't matter at the end of the day: _do_ read the spec of, _do not_ assume anything about a method ;) From the perspective of the tree, an expanded subtree under a collapsed node is irrelevant .. so both naming and implementation look okay for me. Anyway, this particular discussion leads nowhere, *off – kleopatra Apr 06 '20 at 08:38

1 Answers1

2

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.

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/211065/discussion-on-answer-by-m-s-how-to-get-the-row-count-of-a-treetableview). – Samuel Liew Apr 06 '20 at 11:42