我在 javafx2 中创建了树视图。我需要编写事件以获取单击的节点名称。
请让我知道该怎么做?
示例代码:
treeView.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue,
Object newValue) {
TreeItem<String> selectedItem = (TreeItem<String>) newValue;
System.out.println("Selected Text : " + selectedItem.getValue());
// do what ever you want
}
});
稍微更新的版本,使用 lambdas 对其进行了一些清理:
treeView.getSelectionModel()
.selectedItemProperty()
.addListener((observable, oldValue, newValue) -> System.out.println("Selected Text : " + newValue.getValue()));
略微更新的版本
treeView.getSelectionModel().selectedItemProperty()
.addListener(new ChangeListener<TreeItem<String>>() {
@Override
public void changed(
ObservableValue<? extends TreeItem<String>> observable,
TreeItem<String> old_val, TreeItem<String> new_val) {
TreeItem<String> selectedItem = new_val;
System.out.println("Selected Text : " + selectedItem.getValue());
// do what ever you want
}
});