Solved

How to edit nodes in JTree?

Posted on 1997-12-11
4
714 Views
Last Modified: 2012-05-04
I need to change the text of the nodes
of a JTree.

How to do this and how to get notified?

Lewis
0
Comment
Question by:Lewis
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1231567
JTree class has getModel() method which returns a tree model class implementing TreeModel interface (by default it returns DefaultTreeModel class object).

In the DefaultTreeModel class there are methods addTreeModelListener() and nodeChanged(). The first one allows you to register listeners interested in tree node changes (the listeners implement treeNodesChanged() method). The second is called when you change a node (I assume it is responsible for notifying the listeners).

There is no standard way of changing tree node text since tree node is a wrapper around user's data (the wrapper, MutableTreeNode, calls getString() method of your data object do decide on the label to display in the tree view if you are using the default cell renderer). You simply change you data object and call nodeChanged() method on a tree model passing the changed node.

Hope that helps.

Michael
0
 

Author Comment

by:Lewis
ID: 1231568
Hi Michael,
so you are saxing there is no way to edit
a node like in the Windows Explorer, where
you simply click on the node, it gets high-
lighted and editable?

Lewis
0
 
LVL 5

Accepted Solution

by:
msmolyak earned 50 total points
ID: 1231569
There is a default mechanism in JFC tree implementation which allows to edit a tree node like you do in Explorer (I thought you are talking about generic changes to a node text to which there is no concrete answer).  If you look at the SampleTree example supplied with JFC you will see a tree with editable nodes. The comoponent responsible for editing the node implements TreeCellEditor and is called BasicTreeCellEditor.  There is a method in the JTree class setEditable(boolean). The default cell editor alternates between label and text field to display a tree node before and during editing. (Click-pause-click takes the node in the editinig mode). I assume the cell editor takes care of notifying the tree of  the name change.
0
 

Author Comment

by:Lewis
ID: 1231570
Sorry for answering so late. I forgot that the question was
still locked. Thanks, I found the Swing example, it helped.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 46
How  Can  I  resolve   this  Error "java.sql.SQLException: Invalid column index" 4 49
arguments to jar 5 35
ejb example issues 3 28
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question