• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to keep change after i edit the Jtree's leaves??

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calling the stopCellEditing method should end the editing session and save the edit value to the model.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Editing in Tree Views
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you want to keep the changes when focus is lost, you just have to call tree.setInvokesStopCellEditing(true)
 
Andrei Pat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much, it works!!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ramswaroop ram,
Your post was moved to a new topic.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rohit Patel,
Your post was moved to a new topic.
 
reply
    Bookmark Topic Watch Topic
  • New Topic