简体   繁体   中英

Javafx TreeView listener

I have a TreeView in my application and I want to add a listener to get the item that is selected. I currently have:

treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {

            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {

            }           
        });

Error received: The method addListener(ChangeListener<? super TreeItem<String>>) in the type ObservableValue<TreeItem<String>> is not applicable for the arguments (new ChangeListener<String>(){})

What is the correct way to add a listener to a TreeView in javafx?

It is being declared as TreeView<String> treeView = new TreeView<String>(object);

The way I was able to fix this issue was by turning it into a lambda expression like this:

treeView.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> { 
// Body would go here

});

I don't know why my first method did not work but this does work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM