File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes how to keep change after i edit the Jtree's leaves?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to keep change after i edit the Jtree Watch "how to keep change after i edit the Jtree New topic
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: 19545
    
  16

ramswaroop ram,
Your post was moved to a new topic.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19545
    
  16

rohit Patel,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to keep change after i edit the Jtree's leaves??
 
Similar Threads
JTree with checkboxes not getting updated question
JTree node as non editable
JTable In JTree
Radiobuttons in JTree
JTree row selection by mouseclick question