在JavaFX TreeView中编辑项目

时间:2014-09-02 12:47:17

标签: javafx javafx-2 javafx-8

我有这个自定义的TreeView代码:

treeView.setCellFactory(new Callback<TreeView<Tree>, TreeCell<Tree>>()
        {
            @Override
            public TreeCell<Tree> call(TreeView<Tree> treeView)
            {
                final TreeCell<Tree> cell = new TreeCell<Tree>()
                {
                    @Override
                    protected void updateItem(Tree item, boolean empty)
                    {
                        super.updateItem(item, empty);
                        if (!empty)
                        {
                            setText(item != null ? item.toString() : "");
                            setGraphic(createImageView(item));
                            setContextMenu(createContextMenuTreeItem(item));
                        }
                        else
                        {
                            setText(null);
                            setGraphic(null);
                            setContextMenu(null);
                        }
                    }
                };

                return cell;
            }
        });

我希望能够使用内容菜单重命名树的节点。你能帮我实现吗?

1 个答案:

答案 0 :(得分:8)

您可以在MenuItem的EventHandler中开始编辑树节点。这是一个示例代码,允许通过双击(我没有尝试改变/阻止它)以及上下文菜单进行基本编辑:

import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.*;
import javafx.util.converter.DefaultStringConverter;

public class TreeViewSample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Tree View Sample");

        TreeItem<String> rootItem = new TreeItem<String> ("Tree");
        rootItem.setExpanded(true);
        for (int i = 1; i < 6; i++) {
            TreeItem<String> item = new TreeItem<String> ("Item" + i);
            rootItem.getChildren().add(item);
        }

        TreeView<String> tree = new TreeView<String> (rootItem);
        tree.setEditable(true);
        tree.setCellFactory(new Callback<TreeView<String>,TreeCell<String>>(){
            @Override
            public TreeCell<String> call(TreeView<String> p) {
                return new RenameMenuTreeCell();
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(tree);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    private static class RenameMenuTreeCell extends TextFieldTreeCell<String> {
        private ContextMenu menu = new ContextMenu();

        public RenameMenuTreeCell() {
            super(new DefaultStringConverter());

            MenuItem renameItem = new MenuItem("Rename");
            menu.getItems().add(renameItem);
            renameItem.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent arg0) {
                    startEdit();
                }
            });
        }

        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);

            if (!isEditing()) {
                setContextMenu(menu);
            }
        }
    }
}

这基本上是带有自定义树单元格的Oracle example 13-1