collapse listener for tree

i am using the tree to add some items like offices , when we expand that item(Office) i am adding the sub items of that item and some leaf nodes(which doesn’t have any children). while expanding first time its is adding the sub items and when close it ,and then if i again i expanding the item there its showing 2times the subitems…
how should i solve my prob?
please post any poc or suggestion with sample code
thnks

Hi,

if I understood correctly you are adding the new items within an ExpandListener. You could just add code to check if the children for that node are already added. From the ExpandEvent you’ll get a reference to the Tree and the itemId that was expanded. Then you could just call Tree.getChildren(itemId) and check if the returned collection already has the nodes you were going to add.

thnks for quick reply…
i just checked it like follows
but it is giving the null pointer exception while expanding only
System.out.println(t1.getChildren(event.getItemid)).isEmpty());---------> i am getting null pointer exception
boolean flag=t1.getChildren(event.getItemid)).isEmpty();
if(!flag){
/***
adding the child nodes and leaf nodes
*/
}
else{
/

all ready addedlike some notification

*/
}
thnks

Yes, if there are no children at all the getChildren method will return null so you have to check it. On the other hand if you just need to know if there are children at all (and don’t need to check for specific children) you should use the hasChildren(Object itemId) which will return just a boolean value.

i am just confused if u dont mind just post some Poc…

Here’s a very quick PoC which should be simple enough to get what I meant:

public class TreetestApplication extends Application {

    @Override
    public void init() {
        Window mainWindow = new Window("Treetest");
        setMainWindow(mainWindow);

        Tree tree = new Tree();
        tree.addItem("root");
        tree.setItemCaption("root", "Root Node");
        tree.setChildrenAllowed("root", true);

        tree.addListener(new Tree.ExpandListener() {

            public void nodeExpand(ExpandEvent event) {
                Tree source = (Tree) event.getSource();
                Object id = event.getItemId();
                // Check here for:
                // - correct expanded node ID
                // - that the node is allowed to have children
                // - that the node does not yet have children
                if ("root".equals(id) && source.areChildrenAllowed(id)
                        && !source.hasChildren(id)) {
                    source.addItem("child1");
                    source.setItemCaption("child1", "Child 1");
                    source.setParent("child1", id);
                }
            }
        });
        mainWindow.addComponent(tree);
    }
}

thanks bro… its working …completed that…
and another thing that popup button is not working … some widget compilation error is coming…if u know just … suggestion 8o

i done like this…
Collectiontemp1=(Collection) t1.getChildren(selectedOffice);
and then checking null…

thanks… for help

Good that you got your Tree issue solved!

As for this, please make sure that you have compiled the widgetset after adding the PopupButton add-on JAR to your WEB-INF/lib. You should check
this page
for the basic instructions. If you get some problems while following the instructions, please start a new thread for those issues, since this really has nothing to do with the topic anymore :slight_smile:

kkkkkkk …:grin: