| Author |
how to keep change after i edit the Jtree's leaves??
|
Michelle Wang
Ranch Hand
Joined: Apr 17, 2007
Posts: 87
|
|
Hi, I have following code can edit the leaf of the JTree, But after i edit it, I found what I updated is lost, How can I persist all changes I made to these edited leaves and nodes(not leaf). ie, to keep all these changes until i update them later. thanks sunny girl import java.awt.BorderLayout; import java.util.EventObject; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.tree.DefaultTreeCellEditor; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.TreeCellEditor; import javax.swing.tree.TreeNode; public class EditLeafSample { public static void main(String args[]) { JFrame frame = new JFrame("Editable Tree"); JTree tree = new JTree(); tree.setEditable(true); DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer(); TreeCellEditor editor = new LeafCellEditor(tree, renderer); tree.setCellEditor(editor); JScrollPane scrollPane = new JScrollPane(tree); frame.getContentPane().add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); } } class LeafCellEditor extends DefaultTreeCellEditor { public LeafCellEditor(JTree tree, DefaultTreeCellRenderer renderer) { super(tree, renderer); } public LeafCellEditor(JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor) { super(tree, renderer, editor); } public boolean isCellEditable(EventObject event) { // Get initial setting boolean returnValue = super.isCellEditable(event); // If still possible, check if current tree node is a leaf if (returnValue) { Object node = tree.getLastSelectedPathComponent(); if ((node != null) && (node instanceof TreeNode)) { TreeNode treeNode = (TreeNode) node; returnValue = treeNode.isLeaf(); } } return returnValue; } }
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
|
Calling the stopCellEditing method should end the editing session and save the edit value to the model.
|
 |
Michelle Wang
Ranch Hand
Joined: Apr 17, 2007
Posts: 87
|
|
Craig, Thanks a lot, but I am newbie to swing, can you help throw more light?? where to call, how to call?? Thanks really appreciate it.
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
Editing in Tree Views
|
 |
Andrei Pat
Greenhorn
Joined: Apr 19, 2007
Posts: 13
|
|
|
if you want to keep the changes when focus is lost, you just have to call tree.setInvokesStopCellEditing(true)
|
 |
Andrei Pat
Greenhorn
Joined: Apr 19, 2007
Posts: 13
|
|
|
if you want to keep the changes when focus is lost, you just have to call tree.setInvokesStopCellEditing(true), otherwise the changes remain only if you press the "Enter" key
|
 |
Michelle Wang
Ranch Hand
Joined: Apr 17, 2007
Posts: 87
|
|
|
Thanks so much, it works!!
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
ramswaroop ram, Your post was moved to a new topic.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
rohit Patel, Your post was moved to a new topic.
|
 |
 |
|
|
subject: how to keep change after i edit the Jtree's leaves??
|
|
|