We help IT Professionals succeed at work.

How to update the text of Node of JTree? (request of code reference)

fungho
fungho asked
on
Medium Priority
300 Views
Last Modified: 2009-07-29
How can I update the text of node of JTree? The JTree is non-editable. I know I should use DefaultMutableTreeNode.


DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getModel().getRoot());

Am I correct? How can I continue to implement it? e.g. change all the text of the tree to capital letters.

Thanks!

Stephen
Comment
Watch Question

Well,
You would better take care about model yourself. If your don't touch your model, you are right--default model consists of DefaultMutableTreeNode's. But there is some trick to get tree view updated. Call nodeChanged after modifications to node. Or use insert/remove functions in DefaultTreeModel to change data.

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
 
CERTIFIED EXPERT

Commented:
In my opinion you want to attach particular object to each one of the nodes in your tree, so... take a look at setUserObject and getUserObject methods of DefaultMutableTreeNode class. To do this, just overwrite toString method of the object that will be attached to your tree node. When you attach object with overwritten method toString(), the return value will be shown as a text of this node.

When you operate with your tree you should opearte with the model of your tree! You should use methods nodeChanged, nodesChanged, removeNodeFromParent(MutableTreeNode node), insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) and s.o.

Best regards,
Valeri
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
If you just want to change all text to appear in upper case then the easiest way to achieve this is to define your own tree cell renderer.

public class MyTreeCellRenderer extends DefaultTreeCellRenderer
{
     
   public Component getTreeCellRendererComponent(
      JTree tree, Object value,
      boolean selected, boolean expanded,
      boolean leaf, int row, boolean hasFocus)
   {
      if (value!=null)
      {
         value = value.toString().toUpperCase();
      }
      return super.getTreeCellRendererComponent(
         tree, value, selected, expanded,
         leaf, row, hasFocus);
   }      
}

Author

Commented:
actually, the capaitalization of all the text in the tree is done by clicking on a button of my application, it's not changed automatically when I start the application. I am trying to do this by the following codes:

DefaultTreeModel data = (DefaultTreeModel)tree.getModel();
DefaultMutableTreeNode node = (DefaultMutableTreeNode)data.getRoot();
node.setUserObject(((String)node.getUserObject()).toUpperCase());
data.nodeChanged(node);

However, there is no response of the tree, do u need extra to refresh the tree?
CERTIFIED EXPERT
Commented:
Did you set the tree model ?
The proposed code will change only the root node of the tree. To change all nodes you need to scan then using recurssion. You can do it in this way :

private void UpdateNode(DefaultMutableTreeNode sourceNode) {
    sourceNode.setUserObject(((String)sourceNode.getUserObject()).toUpperCase());

    for (Enumeration enum = sourceNode.children(); enum.hasMoreElements();) {
      UpdateNode((DefaultMutableTreeNode)enum.nextElement()));
    }    
  } //end of UpdateNode

Best Regards,
Valeri
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Do you want to change the actual data, or are you just interested in how it is displayed?
If you're only interested in how it's displayed then you're better off using a renderer as I described above.
Otherwise you not only have to enumerate thru all the tree nodes (which could become expensive), but you also have to remember to convert any new nodes.

Using a renderer avoids these issues.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
The renderer would also allow you to do things such as switch from upper back to lowercase and visa versa.

Author

Commented:
Dear Object,

I am only interested in displaying only.

I have a question about the renderer u suggested!

For the renderer, when it will be executed, how can I trigger it?


Thanks!
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
You just need to tell the tree to use your renderer to paint the nodes.

tree.setCellRenderer(new MyTreeCellRenderer());

Whenever the tree needs to renderer a node, your renderer will then be called to retrieve a component to paint the node.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.