看来,要编辑的名字
Resource在
DefaultMutableTreeNode。正如您看到的,的源
ChangeEvent发送到
editingStopped()在没有一个
DefaultTreeCellEditor; 它是编辑器的(匿名)UI委托。
而是getCellEditorValue()在您的中进行覆盖DefaultTreeCellEditor,如下所示。在DefaultTreeCellRenderer简单地调用toString(),通过convertValueToText(),它访问的用户对象DefaultMutableTreeNode。
附录:请注意,isCellEditable()确保只能编辑叶节点。
正如@kleopatra在注释中指出的那样,先前的TreeCellEditor实现无效,因为它修改了正在编辑的节点。下面的修订版本创建了一个具有更新名称的新节点;一个拷贝构造函数是在这方面非常有用。好处是userObject遗迹仍然是Resource。另请参见此替代方法。
public class TreeEditDemo extends JPanel { private JTree tree; private DefaultMutableTreeNode root; private DefaultTreeCellEditor editor; private JLabel label = new JLabel(" ", JLabel.CENTER); public TreeEditDemo() { super(new BorderLayout()); root = new DefaultMutableTreeNode("Nodes"); root.add(new DefaultMutableTreeNode(new Resource("one"))); root.add(new DefaultMutableTreeNode(new Resource("two"))); root.add(new DefaultMutableTreeNode(new Resource("three"))); final DefaultTreeModel treeModel = new DefaultTreeModel(root); tree = new JTree(treeModel); tree.setEditable(true); editor = new MyTreeCellEditor(tree, (DefaultTreeCellRenderer) tree.getCellRenderer()); tree.setCellEditor(editor); tree.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "startEditing"); this.add(new JScrollPane(tree)); this.add(label, BorderLayout.SOUTH); tree.addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { TreePath path = e.getNewLeadSelectionPath(); if (path != null) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); if (node.isLeaf()) { Resource user = (Resource) node.getUserObject(); label.setText(user.toString()); } else { label.setText(" "); } } } }); editor.addCellEditorListener(new CellEditorListener() { @Override public void editingStopped(ChangeEvent e) { label.setText(editor.getCellEditorValue().toString()); } @Override public void editingCanceled(ChangeEvent e) { } }); } private static class MyTreeCellEditor extends DefaultTreeCellEditor { public MyTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer) { super(tree, renderer); } @Override public Object getCellEditorValue() { String value = (String) super.getCellEditorValue(); return new Resource(value); } @Override public boolean isCellEditable(EventObject e) { return super.isCellEditable(e) && ((TreeNode) lastPath.getLastPathComponent()).isLeaf(); } } private static class Resource { String name; public Resource(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return getName(); } } private void display() { Jframe f = new Jframe("TreeEditorDemo"); f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new TreeEditDemo().display(); } }); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)