Search code examples
javaswingeditorrenamejtree

swing - Triggering Tree Cell Edit Event


I have a JTree with editable nodes.

How can I programmatically trigger the tree cell edit event, i.e. bring up the node-renaming textbox in place of a highlighted node, as if the user manually highlighted it and pressed F2?

Basically I want to add a "Rename" menu item or toolbar button, to clue users in on that particular function of the tree, and I want it to function identically to an F2 keypress when the user highlights a node.


Solution

  • 1) some node is selected (by Mouse / KeyBoard event) and by listening by TreeSelectionListener, then selected path has unique ID

    2) add Swing Action to JMenuItem (in JPopup???, not clear from your question, how to get node from /to ???)

    3) create class, void, whatever and to fire

    SwingUtilities.invokeLater(new Runnable() {  
        public void run() {  
            tree.startEditingAtPath(path);  
        }  
    });
    

    4) based on answer by @Michael Dunn to my question on another forum