damixa
asked on
Dynamically Update Elements in JTree
I have a tree let say
A
B
C
D
E
F
When I click on f.e B in another Panel I see the value of B in a text field, what I need to know
if there is a way that once I change the value in text field for B, it will automatically change the value for B in the tree.
Im not sure that there is any update function for Jtrees
thanks
A
B
C
D
E
F
When I click on f.e B in another Panel I see the value of B in a text field, what I need to know
if there is a way that once I change the value in text field for B, it will automatically change the value for B in the tree.
Im not sure that there is any update function for Jtrees
thanks
How are you setting up the JTree at the moment? Can you post the code that you have?
ASKER
Part of the code as this is a big app.
but i extracted the code that is related to the tree.
DefaultMutableTreeNode robotsNode = getRobotTree();
//model = new XMLTreeModel();
tree = new JTree(model);
model = new DefaultTreeModel(robotsNod e);
******************
tree.setModel(model);
tree.setShowsRootHandles(t rue);
tree.setEditable(false);
************************** ********** *
tree.addTreeSelectionListe ner(new TreeSelectionListener() {
public void valueChanged(TreeSelection Event e) {
DefaultMutableTreeNode selectedNode = getSelectedNode();
if (selectedNode!=null) {
if (getNodeType(selectedNode) =="Root" ) {
addShapeButton.setEnabled( false);
addComponentButton.setEnab led(true);
}
else {
addComponentButton.setEnab led(false) ;
addShapeButton.setEnabled( true);}
String View;
System.out.println("Clicke d;" + getNodeName(selectedNode)) ;
if (getNodeType(selectedNode) =="Root")
View=" Root ";
else
View= getNodeName(selectedNode);
tFields.setText(View);
}}
});
private static String getNodeName(DefaultMutable TreeNode TNode) {
String nodeString=" ";
String[] tmp= TNode.getUserObject().toSt ring().spl it(" ");
if (tmp.length>1)
nodeString=tmp[1];
return nodeString;
}
private static DefaultMutableTreeNode getSelectedNode() {
return (DefaultMutableTreeNode) tree.getLastSelectedPathCo mponent();
}
private static DefaultMutableTreeNode getRobotTree() {
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Ro ot");
return rootNode;
}
but i extracted the code that is related to the tree.
DefaultMutableTreeNode robotsNode = getRobotTree();
//model = new XMLTreeModel();
tree = new JTree(model);
model = new DefaultTreeModel(robotsNod
******************
tree.setModel(model);
tree.setShowsRootHandles(t
tree.setEditable(false);
**************************
tree.addTreeSelectionListe
public void valueChanged(TreeSelection
DefaultMutableTreeNode selectedNode = getSelectedNode();
if (selectedNode!=null) {
if (getNodeType(selectedNode)
addShapeButton.setEnabled(
addComponentButton.setEnab
}
else {
addComponentButton.setEnab
addShapeButton.setEnabled(
String View;
System.out.println("Clicke
if (getNodeType(selectedNode)
View=" Root ";
else
View= getNodeName(selectedNode);
tFields.setText(View);
}}
});
private static String getNodeName(DefaultMutable
String nodeString=" ";
String[] tmp= TNode.getUserObject().toSt
if (tmp.length>1)
nodeString=tmp[1];
return nodeString;
}
private static DefaultMutableTreeNode getSelectedNode() {
return (DefaultMutableTreeNode) tree.getLastSelectedPathCo
}
private static DefaultMutableTreeNode getRobotTree() {
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Ro
return rootNode;
}
ASKER
Now what I want to do that when i change the value in the text field tField the value is directly changed in the tree.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Also, you will have to call repaint() on the tree after you update the node's userObject
ASKER
Thanks, I used just an actionperformed and worked fine, as I have no experience with document listeners. Well If you objects If you have a simple document listener example I can open another question so you can get other points. for this one I will give most of the points to Mccarl. hope it is ok for you.