expand TreeView (parent)nodes which contains selected item

跟風遠走 提交于 2019-12-05 12:49:13

Ok, I notice that in your expandTreeView() method you expand the node and then you recurse to the previous node to expand that. In my own code I expanded from the root to the leaf, so lets try that:

private void expandTreeView( TreeItem<User> selectedItem ) 
{
    if ( selectedItem != null ) 
    {
        expandTreeView( selectedItem.getParent() );

        if ( ! selectedItem.isLeaf() ) 
        {
            selectedItem.setExpanded( true );
        }
    }
}

No I don't think its because your method is returning void. Try setting your TreeView root first before expanding:

>>> tvUsers.setRoot(rootItem);

if (selectedUserTreeItem != null) {
    expandTreeView(selectedUserTreeItem);
}

If that doesn't work then try wrapping your initial expandTreeView() to run later:

Platform.runlater( new Runnable()
{
    public void run() { expandTreeView( selectedUserTreeItem ); }
};

Ok, I had the same problem and found that I had to delay the expansion a bit like this:

    Platform.runlater( new Task<Void>()
    {
        @Override
        protected Void call() throws Exception
        {
            Thread.sleep( 250 );
            return null;
        }

        @Override
        protected void succeeded()
        {
            expandTreeView( selectedUserTreeItem );
        }

    } );

You may have to try and vary the delay to get it to work in your case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!