How to get current TreeItem reference which is expanding by user click in JavaFx 2?

眉间皱痕 提交于 2020-01-03 15:23:34

问题


In one JavaFx 2 case, i have a TreeView, which is collapsed at certain depth initially. What i'm aiming to achieve is that when one node is being expanded by user click, an action will happend to modified that treeitem property/values.

the problem is that i'm not sure how to refer to that treeitem. I add a listener on each treeitem expanedProperty, but in that ChangeListner method, i can only get old/new values, can't refer back to the treeitem that change event originates.

Can someone help? Thanks a lot.


回答1:


Yes of course. Try this:

treeItem.expandedProperty().addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
        System.out.println("newValue = " + newValue);
        BooleanProperty bb = (BooleanProperty) observable;
        System.out.println("bb.getBean() = " + bb.getBean());
        TreeItem t = (TreeItem) bb.getBean();
        // Do whatever with t
    }
});


来源:https://stackoverflow.com/questions/14236666/how-to-get-current-treeitem-reference-which-is-expanding-by-user-click-in-javafx

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