repaint tree when node changed
i have a TreeNode class
public MyNodeextends DefaultMutableTreeNode{
private JCheckBox checkbox =null;
public MyNode(String text, JLabel icon){
checkbox =new JCheckBox(text);
JPanel panel =new JPanel();
panel.add(icon);
panel.add(checkbox);
}
publicboolean isSelected(){
return checkbox.isSelected();
}
publicvoid setSelected(boolean selected){
checkbox.setSelected(selected);
// disable all children node
for (Enumeration e = getChildrens(); e.hasMoreElements(); ){
MyNode node = (MyNode) e.nextElement();
node.setEnabled(selected);
}
}
}
When the user checked or unchecked the checkbox, the children node get enabled or disabled. The problem is, the children checkbox is enabled, disabled, but the tree does not update the GUI until the user click on the children node.
I would like to keep the MyNode class not knowing anything about the TreeModel and the JTree. The TreeModel have a TreeModelListener for nodeChanges(), but this method is never invoked when the checkbox is selected. is there a way that i can fire a nodeHasChange?
oops.forgot to mention that the panel is added to the node userObject
public MyNode(String text, JLabel icon){
checkbox = new JCheckBox(text);
JPanel panel = new JPanel();
panel.add(icon);
panel.add(checkbox);
this.setUserObject(panel);
}
>> DefaultTreeModel has nodeChanged() method
yes, but my User Object is JPanel of Swing components..the only thing that changes is the checkbox select property. When the checkbox state changes, the DefaultTreeModel cannot pick up this changes..thus, never fire a node structure change for the tree to perform an update..
I even overide that method..just put a System.out.println to see if the method was fired...but it was never called..In fact..i ut the println in every method..and no method was ever called when the checkbox property changed.
I resorted to provides a method
public void addChangeListener(ChangeListener l)
to the tree node..this would perform
checkbox.addChangeListener(l);
after creating the model, the tree would call get root() and
traverse over all nodes and add a ChangeListener to each node
ChangeListener l = new ChangeListener(){
public void stateChanged(ChangeEvent e){
repaintTree();
}
}
for (int i = 0; i < nodes[]; i++){
node[i].addChangeListener(l);
}
it's a hack. so it's not an elegant fix