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)tr ee.getMode l().getRoo t());
Am I correct? How can I continue to implement it? e.g. change all the text of the tree to capital letters.
Thanks!
Stephen
DefaultMutableTreeNode node = (DefaultMutableTreeNode)tr
Am I correct? How can I continue to implement it? e.g. change all the text of the tree to capital letters.
Thanks!
Stephen
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(Mutab leTreeNode node), insertNodeInto(MutableTree Node newChild, MutableTreeNode parent, int index) and s.o.
Best regards,
Valeri
When you operate with your tree you should opearte with the model of your tree! You should use methods nodeChanged, nodesChanged, removeNodeFromParent(Mutab
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 getTreeCellRendererCompone nt(
JTree tree, Object value,
boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus)
{
if (value!=null)
{
value = value.toString().toUpperCa se();
}
return super.getTreeCellRendererC omponent(
tree, value, selected, expanded,
leaf, row, hasFocus);
}
}
public class MyTreeCellRenderer extends DefaultTreeCellRenderer
{
public Component getTreeCellRendererCompone
JTree tree, Object value,
boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus)
{
if (value!=null)
{
value = value.toString().toUpperCa
}
return super.getTreeCellRendererC
tree, value, selected, expanded,
leaf, row, hasFocus);
}
}
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.get Model();
DefaultMutableTreeNode node = (DefaultMutableTreeNode)da ta.getRoot ();
node.setUserObject(((Strin g)node.get UserObject ()).toUppe rCase());
data.nodeChanged(node);
However, there is no response of the tree, do u need extra to refresh the tree?
DefaultTreeModel data = (DefaultTreeModel)tree.get
DefaultMutableTreeNode node = (DefaultMutableTreeNode)da
node.setUserObject(((Strin
data.nodeChanged(node);
However, there is no response of the tree, do u need extra to refresh the tree?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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!
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.
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.
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