JavaFx 2.0: get TreeItems or Nodes currently visible on screen


Question

I have TreeView<TitledPane> and want to show only "unread" content of TitledPane.

I would like to have some event to be notified if content of TitledPane is visible on screen.

TitledPane has expandedProperty(), so collapsed, but i do not see anything to filter out TreeItems that are far away on TreeView and not displayed.

Another potential way i thought about is to check visibility of node sitting in TitledPane using visibleProperty, but that seems to not work.

pane.visibleProperty().addListener(new EnhancedListener(pane));

private class EnhancedListener implements ChangeListener<Boolean>
{
    Node parent;
    EnhancedListener(Node parent)
    {
        this.parent = parent;
    }
    @Override
    public void changed(ObservableValue<? extends Boolean> arg0, 
                        Boolean arg1, Boolean arg2) {
        TitledPane p = (TitledPane) parent;
        System.out.println(((Label)p.getContent()).getText()+" " + arg2);
    } 
}

I use JavaFX 2.0.3

1
3
4/11/2012 8:00:34 PM

See this JavaFX forum thread "ListView Visible Items" and the related JavaFX issue database entry "Item visibility in scrollable components should be in API" for a request for enhancement to make this easier to achieve what you seek.

The visibleProperty is to do with whether it is possible for an item to be seen when added to a scene, not whether it is currently being displayed (e.g. if a clip or scroll of the layout node currently prevents a child node from being displayed, the child node can still have a visibleProperty value of true, even though you can't currently see it). So in short, as you discovered, it won't help you here.

The above linked forum thread provides the following workaround for ListView, which you may be able to adapt to your TreeView situation, though you would need to work out some way to know that the TreeView's rendered cells displayed on the scene are changing to trigger the "event" you require.

VirtualFlow vf = ((VirtualFlow)((ListViewSkin)myList.getChildrenUnmodifiable().get(0)).getChildrenUnmodifiable().get(0));
System.out.println(vf.getFirstVisibleCell().getIndex()+", "+vf.getLastVisibleCell().getIndex());
4
4/17/2012 12:23:24 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon