Editing JTree Nodes

J

Jason Cavett

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.)
 
T

tomaszewski.p

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);
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top