Solved

How to edit nodes in JTree?

Posted on 1997-12-11
4
690 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 61
count8 challlenge 13 86
mapShare challenge 13 69
What is the latest versions eclipse neon 2 122
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now