5

With the standard GWT 2.0.3 API, how do you add a Clickhandler to the TreeItem? I'm hoping to implement asynchronous calls to the server that will retrieve the resulting TreeItems that are expanded.

Unfortunately FastTree doesn't work in GXT applications. Therefore I'm back to the original step of needing to attach handlers to TreeItems!

Are there any noticeable downfalls with this code:

Tree.addSelectionHandler(new SelectionHandler<TreeItem>()
{
    @Override
    public void onSelection(SelectionEvent event()
    {
        if(event.getSelectedItem == someTreeItem)
        {
            //Something
        }
    }
});
Federer
  • 33,677
  • 39
  • 93
  • 121

2 Answers2

12

With GWT's default Tree, there aren't handlers for specific TreeItems, just one SelectionHandler for the entire tree:

tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
  @Override
  public void onSelection(SelectionEvent<TreeItem> event) {
    TreeItem item = event.getSelectedItem();
    // expand the selected item
  }
});

However, the GWT incubator's FastTree was literally built just for what you're trying to do, lazy-load the tree as items are expanded, so I would start there. Feel free to ask more questions if you have any.

Matt McMinn
  • 15,983
  • 17
  • 62
  • 77
Jason Hall
  • 20,632
  • 4
  • 50
  • 57
-1
// First create a new treeitem-class with a new method:
public class TreeItemAdv extends TreeItem {
    protected void doSelectionAction() {
        // TODO: The child should overwrite this method!
        System.out.println("The child should overwrite this method!");
    }
}

...
    // define your tree:
    Tree tree = new Tree();
    tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
        @Override
        public void onSelection(SelectionEvent<TreeItem> event) {               
            TreeItemAdv item = (TreeItemAdv) event.getSelectedItem();
            item.doSelectionAction(); // do item-specific stuff
        }
    });

    // define and add your items:
    TreeItemAdv ti1 = new TreeItemAdv() {
        @Override
        protected void doSelectionAction() {
            // TODO: Do some stuff.
            System.out.println("1: Here I am.");
        }           
    };      
    ti1.setText("Item 1");      
    tree.addItem(ti1);
    // and an other item:
    TreeItemAdv ti2 = new TreeItemAdv() {
        @Override
        protected void doSelectionAction() {
            // TODO: Do some stuff.
            System.out.println("2: Here I am.");
        }           
    };      
    ti2.setText("Item 2");      
    tree.addItem(ti2);
...