Link to home
Start Free TrialLog in
Avatar of fungho
fungho

asked on

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

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
Avatar of Igor Bazarny
Igor Bazarny
Flag of Switzerland image

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
 
Avatar of Valeri
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
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);
   }      
}
Avatar of fungho
fungho

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Valeri
Valeri
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
The renderer would also allow you to do things such as switch from upper back to lowercase and visa versa.
Avatar of fungho

ASKER

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!
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.