?
Solved

How to edit nodes in JTree?

Posted on 1997-12-11
4
Medium Priority
?
742 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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 …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

765 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