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

[1845 byte] By [Sergey.Chunayeva] at [2007-11-27 11:38:14]
# 1

I don't understand the question. The DefaultTreeModel has methods allowing your to insert and remove nodes. And the JTree tutorial has example code showing you how to use these methods.

camickra at 2007-7-29 17:19:03 > top of Java-index,Desktop,Core GUI APIs...
# 2

Have you tried

treeModel.removeNodeFromParent( node );

Where node is a implementation of TreeNode?

ICE

icewalker2ga at 2007-7-29 17:19:03 > top of Java-index,Desktop,Core GUI APIs...
# 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

Sergey.Chunayeva at 2007-7-29 17:19:03 > top of Java-index,Desktop,Core GUI APIs...
# 4

I think there are no chances to do with model implementations in my case. And the only right solution is to code additional adapters for ProductCategory class which extends DefaultMutableTreeNode. Then I need to code a logic for adding nodes to tree, to remove them and so on. But this is a boring task. I thought that table models are exists for this kind of work.

Sergey.Chunayeva at 2007-7-29 17:19:03 > top of Java-index,Desktop,Core GUI APIs...