Editing JTree Nodes

Discussion in 'Java' started by Jason Cavett, Sep 15, 2008.

  1. Jason Cavett

    Jason Cavett Guest

    So I have implemented my own tree cell editor (called
    ProjectTreeCellEditor) by extended DefaultTreeCellEditor so I could do
    a couple customized things upon editing.

    However, when I created the ProjectTreeCellEditor, I also implemented
    the CellEditorListener interface so, when editing is cancelled or
    stopped, I can update the actual data. However, I was reading that
    the data update should not take place in the cell editor (which makes
    sense since its data is returned via the "cellEditorValue"). What's
    confusing, though, is that the same book I was reading said its rare
    that you need to implement your own CellEditorListener. Is this
    true? That seems odd since I don't know how data would be set to the
    actual object once the editing has stopped.

    Can anybody provide insight to this. (What I have now does work, but
    this new information has me a little puzzled.)
     
    Jason Cavett, Sep 15, 2008
    #1
    1. Advertising

  2. On 15 Wrz, 18:37, Jason Cavett <> wrote:
    > So I have implemented my own tree cell editor (called
    > ProjectTreeCellEditor) by extended DefaultTreeCellEditor so I could do
    > a couple customized things upon editing.
    >
    > However, when I created the ProjectTreeCellEditor, I also implemented
    > the CellEditorListener interface so, when editing is cancelled or
    > stopped, I can update the actual data.  However, I was reading that
    > the data update should not take place in the cell editor (which makes
    > sense since its data is returned via the "cellEditorValue").  What's
    > confusing, though, is that the same book I was reading said its rare
    > that you need to implement your own CellEditorListener.  Is this
    > true?  That seems odd since I don't know how data would be set to the
    > actual object once the editing has stopped.
    >
    > Can anybody provide insight to this.  (What I have now does work, but
    > this new information has me a little puzzled.)


    I am not sure I have it clear (what do you mean by 'data update'), but
    I have implemented short test program which displays value from tree
    model after editing of item is done. It looks like tree model is
    already updated, and tree displays changed value.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;

    import java.util.EventObject;

    import javax.swing.DefaultCellEditor;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JTree;

    import javax.swing.event.CellEditorListener;
    import javax.swing.event.ChangeEvent;

    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellEditor;

    public class JTreeCellEditor {

    static class CustomCellEditor extends DefaultCellEditor {

    public CustomCellEditor() {
    super(new JTextField());
    }

    @Override
    public Component getTreeCellEditorComponent(JTree tree, Object
    value, boolean isSelected, boolean expanded, boolean leaf, int row) {
    JTextField editor = (JTextField) editorComponent;
    editor.setText('-' + value.toString() + '-');
    if (isSelected) {
    editor.setBackground(Color.YELLOW);
    }
    return editor;
    }
    }

    static class TreePanel extends JPanel {
    private DefaultTreeModel treeModel;
    private DefaultMutableTreeNode rootNode;
    private DefaultMutableTreeNode childNode;

    public TreePanel() {
    setLayout(new BorderLayout());
    rootNode = new DefaultMutableTreeNode("Root");
    childNode = new DefaultMutableTreeNode("Child1");
    rootNode.add(childNode);
    treeModel = new DefaultTreeModel(rootNode);

    JTree tree = new JTree(treeModel);
    DefaultTreeCellEditor cellEditor = new
    DefaultTreeCellEditor(tree, new DefaultTreeCellRenderer(), new
    CustomCellEditor());
    cellEditor.addCellEditorListener(new CellEditorListener()
    {
    public void editingCanceled(ChangeEvent event) {
    }

    public void editingStopped(ChangeEvent event) {
    System.out.println("Child in tree model = " +
    ((DefaultMutableTreeNode) treeModel.getChild(rootNode,
    0)).getUserObject());
    ` }
    });
    tree.setCellEditor(cellEditor);
    tree.setEditable(true);
    add(tree, BorderLayout.CENTER);

    setPreferredSize(new Dimension(200, 400));
    }
    }

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new TreePanel());
    frame.pack();
    frame.setVisible(true);
    }
    }
     
    tomaszewski.p, Sep 16, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnny
    Replies:
    0
    Views:
    922
    Johnny
    Oct 2, 2003
  2. Clive Moore

    Hide Leaf nodes in a jtree

    Clive Moore, Oct 29, 2003, in forum: Java
    Replies:
    1
    Views:
    852
    Harald Hein
    Oct 30, 2003
  3. dablick
    Replies:
    0
    Views:
    520
    dablick
    Dec 12, 2003
  4. Replies:
    1
    Views:
    4,009
    Thomas Weidenfeller
    Mar 31, 2005
  5. jumpz
    Replies:
    3
    Views:
    649
    Jean-Baptiste Nizet
    Nov 12, 2007
Loading...

Share This Page