Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Editing JTree Nodes

Reply
Thread Tools

Editing JTree Nodes

 
 
Jason Cavett
Guest
Posts: n/a
 
      09-15-2008
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.)
 
Reply With Quote
 
 
 
 
tomaszewski.p
Guest
Posts: n/a
 
      09-16-2008
On 15 Wrz, 18:37, Jason Cavett <(E-Mail Removed)> 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_CLOS E);
frame.add(new TreePanel());
frame.pack();
frame.setVisible(true);
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JTree Selection lost when JTree looses focus jumpz Java 3 11-12-2007 03:55 PM
change the icon of a JTree parent node according to its child nodes esamsalah@gmail.com Java 1 03-31-2005 02:07 PM
Automatic expansion of "inserted" JTree nodes dablick Java 0 12-12-2003 04:24 PM
Hide Leaf nodes in a jtree Clive Moore Java 1 10-30-2003 06:23 AM
JTree cut single and multiple child and ancestor nodes not function the right way! Johnny Java 0 10-02-2003 06:44 PM



Advertisments