Solved

How to edit nodes in JTree?

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

688 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