使用 JTable 作为 JTree 单元格编辑器

2024-03-12

我想使用 JTable 来编辑 JTree,我扩展了 DefaultTreeCellEditor 并实现了 isCellEditable getTreeCellEditorComponent,在 getTreeCellEditorComponent 中我返回了一个 JTable。到此为止,当编辑节点时,swing 显示填充有对象内容的 JTable,但是当编辑完成时,DefaultTreeModel 的 valueForPathChanged 永远不会被调用。如果我使用默认的文本字段进行编辑,一切都会正常。


JTextField has a notifyAction https://docs.oracle.com/javase/8/docs/api/javax/swing/JTextField.html#notifyAction,名为“notify-field-accept”,通常绑定到KeyEvent.VK_ENTER,这标志着CellEditor to stopEditing()并最终调用DefaultTreeCellEditor方法,valueForPathChanged() https://docs.oracle.com/javase/8/docs/api/javax/swing/tree/DefaultTreeModel.html#valueForPathChanged-javax.swing.tree.TreePath-java.lang.Object-.

目前尚不清楚您如何表明编辑已完成JTable。你应该能够做类似的事情JTextField in a CellEditorListener https://docs.oracle.com/javase/8/docs/api/javax/swing/event/CellEditorListener.html通过以下方式添加到您的自定义编辑器addCellEditorListener() https://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractCellEditor.html#addCellEditorListener-javax.swing.event.CellEditorListener-.

顺便,valueForPathChanged() https://docs.oracle.com/javase/8/docs/api/javax/swing/tree/DefaultTreeModel.html#valueForPathChanged-javax.swing.tree.TreePath-java.lang.Object-提到“如果您在 TreeModel 中使用自定义用户对象,您将需要对其进行子类化并将已更改节点的用户对象设置为有意义的内容。”

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 JTable 作为 JTree 单元格编辑器 的相关文章

随机推荐