JavaFX 2 TreeView change Focus from TreeView TreeCell


Question

Hello I´m trying to implement a TreeView which is capable of creating a new Element via ContextMenu in the TreeView. However I want the new element to be focused to call the startEdit() method and give it a name myself. Is there any possible way to do so...

Here´s my code:

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();
    }
    }
}
1
0
7/10/2012 6:50:19 AM

Newly created item can be focused either by selecting the created item or the index of the item. Selection can be done as follows.

treeview.getSelectionModel().select(new_item)

or

treeview.getSelectionModel().select(new_item_index)
1
5/7/2013 3:22:01 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon