swing - Deleting node childs of a Java JTree structure -


i have ftp program retrieve folder data each time expanded. using model this:

     private void filestreetreeexpanded(javax.swing.event.treeexpansionevent evt) {
string path = new string("");

 defaultmutabletreenode chosen = (defaultmutabletreenode) evt.getpath().getlastpathcomponent();   string[] patharray = evt.getpath().tostring().replaceall("]", "").split(",");  (int = 1 ; < patharray.length ; i++) path += "/"+ patharray[i].trim(); 

// aded chosen.removeallchildren(); without success ftp.goto(path);

arraylist listdir = null; listdir = ftp.listdir(); arraylist listfiles = null; listfiles = ftp.listfiles(); defaultmutabletreenode child = null , dir = null , x = null; //this add files tree (int = 0; < listfiles.size(); i++) { child = new defaultmutabletreenode(listfiles.get(i)); if(listfiles.size() > 0) model.insertnodeinto(child, chosen, 0); } //this add dirs list (int = 0; < listdir.size(); i++) { x = new dirbranch("در حال دریافت اطلاعات ...").node(); dir = new dirbranch( (string) listdir.get(i)).node(); dir.add(x); if(listdir.size() > 0) model.insertnodeinto(dir, chosen, 0); } filestree.setmodel(model); //this swing jtree }

the problem every time expand jtree duplicate list of files , folders. tried use chosen.removeallchildren(); @ top of code didnt remove anything. should do?

your model correct, jtree operating on old information.

the removeallchildren() method removes children, not fire events , model.insertnodeinto() fire insert events. so, jtree sees nodes being added, never sees nodes being removed.

after adding new children, try calling model.reload(chosen) invalidate tree below chosen.

since reloading branch, can change model.insertnodeinto(dir, chosen,0) chosen.insert(dir,0). reduces number of events posted.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -