日期:2014-05-20  浏览次数:20736 次

java中如何刷新JTree的节点信息??
JTree这样创建:
    DefaultMutableTreeNode   biology   =   new   DefaultMutableTreeNode   ( "Biology ");
    DefaultMutableTreeNode   animal   =   new   DefaultMutableTreeNode   ( "Animal ");
    DefaultMutableTreeNode   mammal   =   new   DefaultMutableTreeNode   ( "Mammal ");
    DefaultMutableTreeNode   horse   =   new   DefaultMutableTreeNode   ( "Horse ");
    mammal.add   (horse);
    animal.add   (mammal);
    biology.add   (animal);
    JTree   tree=   new   JTree   (biology);

JScrollPane   jsp   =   new   JScrollPane(jsp);
..
如果要对tree的全部节点重新赋值,该怎么个写法?还有就是更新节点后怎么写代码来动态更新tree控件?
急!!!。希望不吝赐教!!!!!

------解决方案--------------------
http://java.sun.com/docs/books/tutorial/uiswing/examples/components/index.html#SharedModelDemo
------解决方案--------------------
new DefaultMutableTreeNode (new Data( "Biology "))

public class Data extends Object{
String newString;
public Data(String newString) {
this.newString=newString;
}

/**
* Sets the string to display for this object.
*/
public void setString(String newString) {
this.newString = newString;
}

/**
* Returnes the string to display for this object.
*/
public String string() {
return this.newString;
}

public String toString() {
return this.newString;
}
}


Data Dt=(Data)getSelectedNode().getUserObject();

Dt.setString( "Your String ");

tree.updatUI();

new_chr2008@163.com
------解决方案--------------------
DefaultMutableTreeNode biology = new DefaultMutableTreeNode ( "Biology ");
DefaultMutableTreeNode animal = new DefaultMutableTreeNode ( "Animal ");
DefaultMutableTreeNode mammal = new DefaultMutableTreeNode ( "Mammal ");
DefaultMutableTreeNode horse = new DefaultMutableTreeNode ( "Horse ");

通过程序得到biology等树结点后,通过setUserObject( "你想显示的内容 ")即可
------解决方案--------------------
Swing基于MVC模型,支持Model修改后自动调用Controller更新View。