0

I want to listen to text changes when the user is editing the DefaultMutableTreeNode when the JTree is set editable. Like, i want to display a status label of what the user is typing in the node.

Unfortunately, i didn't find a DocumentListener for DefaultMutableTreeNode to listen to changes like inserting, deleting and modifying text in the DefaultMutableTreeNode.

Could any one say me how to do this? Any working answer is appreciated. Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
JavaTechnical
  • 8,846
  • 8
  • 61
  • 97

1 Answers1

1

Once the editor is prepared, you can add a DocumentListener to the cell editor's editingComponent. Starting from this example, add the following method to MyTreeCellEditor:

@Override
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
    final Component c = super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
    JTextField jtf = (JTextField) editingComponent;
    jtf.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            print(e);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            print(e);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            print(e);
        }
        private void print(DocumentEvent e) {
            System.out.println(e);
        }
    });
    return c;
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045