Problem to update JTree
Hello world :)
I use JDK 1.6 update 2. My problem description:
I have a database with product categories. Each category can have unlimited subcategories. For displaying it's structure in a JTree I've extended DefaultTreeModel, override some necessary methods and everything works fine.
But when a user add or delete a new category or a subcategory it is deleted in a database. I can not find the way to update only this parent node, because this table model needs this node to be an implementation of a TreeNode. The only solution that works and is not a good one is to set each time a new TableModel. But even in this case a JTree collapses all expanded nodes and this is a bad thing that exasperates a user. Then I've tried to remember a TreePath of parent node and after setting a new model to call expandPath for it. But this gives nothing, all stays collapsed. I've tried to create a "shell" for my ProductCategory entity, named it TreeProductCategory, it extends a DefaultMutableTreeNode and composites my ProductCategory delegating all necessary methods. But in this case I got exceptions that this node doesn't have children when delete it. I confused and bog down.
//Here I run over all selected tree paths
for(TreePath path : paths){
//get ProductCategory from node
ProductCategory category = (ProductCategory)path.getLastPathComponent();
//data is deleting from a database.
//But how to update a parent tree node and notify it that
//child node was deleted?
ProductCategoryManager.getInstance().deleteByCrieteria_ProductCategory(category);
}
//now I just set a new model. And get collapsed nodes.
jTreeCategory.setModel(new TreeProuductCategoryModel());
Message was edited by:
Sergey.Chunayev
# 3
> Have you tried
> treeModel.removeNodeFromParent( node );
> Where node is a implementation of TreeNode?
>
> ICE
Thanks for solution. Here is my implementation of tree model.
public class TreeProductCategoryModel extends DefaultTreeModel {
private TreeProductCategoryRoot root;
/** Creates a new instance of TreeProductCategoryModel */
public TreeProductCategoryModel(TreeNode node){
super(node);
}
public TreeProductCategoryModel(){
this(new TreeProductCategoryRoot());
}
public TreeProductCategoryModel(TreeProductCategoryRoot root){
super((TreeNode)root);
if(root == null){
this.root = new TreeProductCategoryRoot();
}else{
this.root = root;
}
}
public Object getRoot() {
return root;
}
public Object getChild(Object parent, int index) {
if(parent instanceof TreeProductCategoryRoot){
TreeProductCategoryRoot r = (TreeProductCategoryRoot)parent;
return r.getAt(index);
}
if(parent instanceof ProductCategory){
ProductCategory c = (ProductCategory)parent;
return ProductCategoryManager.getInstance().getChildren(c).toArray()[index];
}
return null;
}
public int getChildCount(Object parent) {
if(parent instanceof TreeProductCategoryRoot){
TreeProductCategoryRoot r = (TreeProductCategoryRoot)parent;
return r.getChildCount();
}
ProductCategory c = (ProductCategory)parent;
return (int)ProductCategoryManager.getInstance().getCountChildren(c);
}
public boolean isLeaf(Object node) {
return (getChildCount(node) == 0);
}
}
My nodes don't implement TreeNode, because they are persistence elements marked @Entity and I don't want to mix their implementation with a view. Is there any other ways to work with a tree without using TreeNode implemantation. I thought that it was enough to write my own class of table model, or I was wrong? I have only a little experience with Swing, may be my code has mistakes? P.S. TreeProductCategoryRoot implements TreeModel. This is done only to have a top most element root with label "Product Categories" and it is not a ProductCategory.
Message was edited by:
Sergey.Chunayev