0

您好,我正在尝试实现一个 TreeView,它能够通过 TreeView 中的 ContextMenu 创建一个新元素。但是,我希望新元素能够集中调用 startEdit() 方法并自己给它起一个名字。有没有什么办法可以做到...

这是我的代码:

public class NotesBrowser extends TreeView<String>
{
    public NotesBrowser()
    {
    this.setEditable(true);

    IconView rootIcon = new IconView(Resources.rootImage);
    rootIcon.setSize(28.5, 28.5);

    ExtendedTreeItem treeRoot = new ExtendedTreeItem("Notes", true);
    treeRoot.setGraphic(rootIcon);
    treeRoot.setIsRoot(true);
    setRoot(treeRoot);

    this.setId("menubar_Notes");
    this.setShowRoot(true);
    this.setRoot(treeRoot);

    this.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>()
    {
        @Override
        public TreeCell<String> call(TreeView<String> p)
        {
        return new TextFieldTreeCellImpl();
        }
    });

    treeRoot.setIsRoot(true);
    treeRoot.setExpanded(true);
    }

    /**
     * TextFieldTreeCellImpl handles actions on TreeView items Core functions: -
     * Right Click on Folder -> create Folder, add Note, delete, rename - Right
     * Click on Note -> add Note, delete, rename
     * 
     * @author Googles
     * @credits Oracle TreeView
     *          Samples(http://docs.oracle.com/javafx/2/ui_controls
     *          /TreeViewSample.java.html)
     * 
     */
    public final class TextFieldTreeCellImpl extends TreeCell<String>
    {
    private TextField textField;

    private ContextMenu noteMenu = new ContextMenu();
    private ContextMenu folderMenu = new ContextMenu();

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public TextFieldTreeCellImpl()
    {
        // Initialize MenuItems
        MenuItem addFolder = new MenuItem("Create Folder");
        MenuItem addNote = new MenuItem("Create Note");
        MenuItem delete = new MenuItem("Delete");
        MenuItem rename = new MenuItem("Rename");

        // Add MenuItem to dedicated ContextMenus
        noteMenu.getItems().addAll(rename, delete);
        folderMenu.getItems().addAll(addNote, addFolder, rename, delete);

        // Set MenuItem actions
        addNote.setOnAction(new EventHandler()
        {
        public void handle(Event t)
        {
            ExtendedTreeItem newNote = new ExtendedTreeItem("New Note", false);
            getTreeItem().getChildren().add(newNote);
            getTreeItem().setExpanded(true);

        }
        });

        rename.setOnAction(new EventHandler()
        {
        @Override
        public void handle(Event t)
        {
            startEdit();
        }
        });

    }

    /**
     * Creates TextField on TreeCell Element to rename it
     */
    @Override
    public void startEdit()
    {
        if (!((ExtendedTreeItem) getTreeItem()).isRoot())
        {
        super.startEdit();
        if (textField == null)
        {
            createTextField();
        }
        setText(null);
        setGraphic(textField);
        textField.selectAll();
        }
    }

    /**
     * Cancel Edit; obvious comment is obvious...
     */
    @Override
    public void cancelEdit()
    {
        super.cancelEdit();

        setText((String) getItem());
        setGraphic(getTreeItem().getGraphic());
    }

    /**
     * Updates TreeCell to new name and attach ContextMenus to different
     * TreeCells
     */
    @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 (((ExtendedTreeItem) getTreeItem()).isDirectory())
            {
            setContextMenu(folderMenu);
            }
            else if (!((ExtendedTreeItem) getTreeItem()).isDirectory())
            {
            setContextMenu(noteMenu);
            }
        }
        }
    }

    /**
     * Initializes TextField and adds onClick actions to handle commit /
     * cancel
     */
    private void createTextField()
    {
        textField = new TextField(getString());
        textField.setOnKeyReleased(new EventHandler<KeyEvent>()
        {
        @Override
        public void handle(KeyEvent t)
        {
            if (t.getCode() == KeyCode.ENTER)
            {
            commitEdit(textField.getText());
            }
            else if (t.getCode() == KeyCode.ESCAPE)
            {
            cancelEdit();
            }
        }
        });
    }

    private String getString()
    {
        return getItem() == null ? "" : getItem().toString();
    }
    }
}
4

1 回答 1

1

可以通过选择创建的项目或项目的索引来聚焦新创建的项目。可以如下进行选择。

treeview.getSelectionModel().select(new_item)

或者

treeview.getSelectionModel().select(new_item_index)
于 2013-05-07T03:01:08.257 回答