Programmatically removing nodes from a JTree

H

helpwithjava

Hello,

I have a JTree application which loads the leaves from a database.
Every time a node is expanded it checks the database and lazily
populates the tree. This works well using a TreeWillExpandListener.
The database can change so when a node is collapsed I want to remove
all the leaves. Then when it is expanded again it will read the
database and show any updates. For the collapse I ahve used a
TreeExpansion listener.

My problem is that when I remove all the leaves from the node on
collapse this node loses the expansion + to the left of it. If I
click where the plus would be then the node does expand and populate
correctly. My problem is the LnF of the node.

My code for collapsing is:

private void removeThisNode(TreePath path) {
TreeNode node = (TreeNode) path.getLastPathComponent();
Enumeration<DefaultMutableTreeNode> children = node.children
();
List<DefaultMutableTreeNode> childrenList = new
ArrayList<DefaultMutableTreeNode>();
while (children.hasMoreElements()) {
DefaultMutableTreeNode object = children.nextElement();
childrenList.add(object);
}
for (MutableTreeNode mutableTreeNode : childrenList) {
defaultTreeModel.removeNodeFromParent(mutableTreeNode);
}
//TODO: why does this get rid of the +
}

I have tried to work out what is causing the + to disappear but cannot
work it out. If I ask the parent node if it is a leaf node it returns
false. So the model knows that leaves can be added to it. It just
loses the +

Thanks for reading and for your "help with java" :)
 
I

info

Hello,

I have a JTree application which loads the leaves from a database.
Every time a node is expanded it checks the database and lazily
populates the tree. This works well using a TreeWillExpandListener.
The database can change so when a node is collapsed I want to remove
all the leaves. Then when it is expanded again it will read the
database and show any updates. For the collapse I ahve used a
TreeExpansion listener.

My problem is that when I remove all the leaves from the node on
collapse this node loses the  expansion + to the left of it. If I
click where the plus would be then the node does expand and populate
correctly. My problem is the LnF of the node.

My code for collapsing is:

 private void removeThisNode(TreePath path) {
        TreeNode node = (TreeNode) path.getLastPathComponent();
        Enumeration<DefaultMutableTreeNode> children = node.children
();
        List<DefaultMutableTreeNode> childrenList = new
ArrayList<DefaultMutableTreeNode>();
        while (children.hasMoreElements()) {
            DefaultMutableTreeNode object = children.nextElement();
            childrenList.add(object);
        }
        for (MutableTreeNode mutableTreeNode : childrenList) {
            defaultTreeModel.removeNodeFromParent(mutableTreeNode);
        }
        //TODO: why does this get rid of the +
    }

I have tried to work out what is causing the + to disappear but cannot
work it out. If I ask the parent node if it is a leaf node it returns
false. So the model knows that leaves can be added to it. It just
loses the +

Thanks for reading and for your "help with java" :)

Looks like a situation where you may need to use a bit of a hack to
achieve your (reasonable) L&F.. when deleting all nodes, why not add
one dummy node so that JTree still thinks there are subnodes? Then
when user clicks node, delete all again, and repopulate with DB data
nodes...
 
H

helpwithjava

Looks like a situation where you may need to use a bit of a hack to
achieve your (reasonable) L&F.. when deleting all nodes, why not add
one dummy node so that JTree still thinks there are subnodes? Then
when user clicks node, delete all again, and repopulate with DB data
nodes...

Hi,

Thanks for the response. I was worried that might be the answer. What
I think I will do is remove all the nodes on the
TreeWillExpandListener.treeWillExpand before I populate it. The
overhead should not be great as the DB communications will take most
of the time.

Thanks
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Jtree renderers 0
JTree oddity... 5
change icon in jtree 0
Updating JTree When Adding Nodes 2
JTable within JTree 1
JTree navigation 1
JTree application 3
need to insert a parent in a jtree 5

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top