如果treeview2中存在树状单元,是否有可能添加一个更改treeview1中树状图形图形的侦听器?如果它被删除,也删除图形?
现在我的代码的简化MCVE,带有一个从一个treeView到另一个树视图添加treeItem的按钮:
Button button2 = new Button("qq");
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
TreeItem<String> treeRoot = new TreeItem<String>("Root Node");
treeRoot.setExpanded(true);
treeRoot.getChildren().addAll(
new TreeItem<String>("Item 1"),
new TreeItem<String>("Item 2"),
new TreeItem<String>("Item 3")
);
TreeView<String> treeView = new TreeView<String>(treeRoot);
TreeItem<String> treeRoot2 = new TreeItem<String>("Root Node2");
treeRoot2.setExpanded(true);
treeRoot2.getChildren().addAll(
new TreeItem<String>("Item 1"),
new TreeItem<String>("Item 2"),
new TreeItem<String>("Item 3")
);
TreeView<String> treeView2 = new TreeView<String>(treeRoot2);
button2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
treeRoot.getChildren().add(treeRoot2.getChildren().get(1));
}
});
StackPane root = new StackPane();
SplitPane splitPane = new SplitPane();
splitPane.getItems().addAll(treeView, treeView2);
root.getChildren().addAll(splitPane);
root.getChildren().add(button2);
Scene scene = new Scene(root, 1000, 1000);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();