Hamza Yerlikaya
Hamza Yerlikaya

Reputation: 49329

Using JTable for a JTree cell editor

I would like to use a JTable for editing a JTree, I extended DefaultTreeCellEditor and implemented isCellEditable getTreeCellEditorComponent, in getTreeCellEditorComponent I return a JTable. Everything works up to this point when a node is edited swing displays the JTable filled with the objects content however when editing is complete, valueForPathChanged of DefaultTreeModel never gets called. If I use a text field for editing which is the default everything works fine.

Upvotes: 2

Views: 1567

Answers (1)

trashgod
trashgod

Reputation: 205785

JTextField has a notifyAction, named "notify-field-accept" and typically bound to KeyEvent.VK_ENTER, that signals the CellEditor to stopEditing() and ultimately invokes the DefaultTreeCellEditor method, valueForPathChanged().

It's not clear how you indicate that editing is complete for your JTable. You should be able to do something similar with the JTextField in a CellEditorListener that is added to your custom editor via addCellEditorListener().

Incidentally, valueForPathChanged() mentions that "If you use custom user objects in the TreeModel you're going to need to subclass this and set the user object of the changed node to something meaningful."

Upvotes: 2

Related Questions