Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to edit nodes in JTree?

Posted on 1997-12-11
4
Medium Priority
?
757 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

636 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