Hi i am working on an application related to treeView in javafx, i asked this question before [1]: JavaFx: How to Use existing branch with leafs as a newly add branch , but didn't get any answer. i tried myself and reached here
public class Main extends Application
{
private static TreeItem<String> item2;
@SuppressWarnings("unchecked")
@Override
public void start(Stage primaryStage)
{
BorderPane border = new BorderPane();
Scene scene = new Scene(border,300,300);
primaryStage.setTitle("BorderPane");
primaryStage.setScene(scene);
primaryStage.show();
TreeItem<String> tree = new TreeItem<String>("Root");
item2 = new TreeItem<String>("poles");
TreeItem<String> item2Child1 = new TreeItem<String>("X");
TreeItem<String> item2Child2 = new TreeItem<String>("Y");
item2.getChildren().addAll(item2Child1,item2Child2);
//item2.setExpanded(true);
tree.setExpanded(true);
tree.getChildren().addAll(item2);
TreeView<String> treeView = new TreeView<String>(tree);
treeView.setCellFactory(new Callback<TreeView<String>,TreeCell<String>>(){
@Override
public TreeCell<String> call(TreeView<String> p) {
return new AddMenuTreeCell();
}
});
border.setCenter(treeView);
}
private static class AddMenuTreeCell extends TextFieldTreeCell<String> {
private ContextMenu menu = new ContextMenu();
private TextField textField;
public AddMenuTreeCell() {
MenuItem item1 = new MenuItem("Insert poles");
menu.getItems().add(item1);
item1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
getTreeItem().getChildren().add(item2);
}
});
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
if (textField != null) {
textField.setText(getString());
}
setText(null);
setGraphic(textField);
} else {
setText(getString());
setGraphic(getTreeItem().getGraphic());
if (!(getTreeItem().isLeaf() && getTreeItem().getParent() == null)){
setContextMenu(menu);
}
}
}
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
}
}
With this user can add existing branch "poles". But the problem is, user cannot add Poles inside any pole branch.
[Application work like this user have to use existing branch "pole" as newly added branch and can add as many poles as he want inside any branch.]
Any good Idea will be helpful Please.
Thank you
答案 0 :(得分:1)
根据需要,新添加的分支在添加时应具有自己的叶子。使用现有分支来解决这个问题是错误的。添加
时应使用新分支 private static class AddMenuTreeCell extends TextFieldTreeCell<String> {
private ContextMenu menu = new ContextMenu();
private TextField textField;
// private TreeItem<String> newBranch;
public AddMenuTreeCell() {
MenuItem renameItem1 = new MenuItem("Insert Unite");
menu.getItems().add(renameItem1);
renameItem1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
TreeItem<String> item3 = new TreeItem<String>("poles");
item3.getChildren().addAll(new TreeItem<>("x"),new TreeItem<>("y"));
getTreeItem().getChildren().add(item3);
}
});
}