3

I've been looking through GXT3's Tree API for some way to execute an action when I click or double click on a node in a tree, and I can't seem to find anything that would work.

I know TreeGrid has a CellClickHandler and CellDoubleClick handler, but there doesn't seem to be anything similar for Tree. There's the generic addHandler method inherited from Widget but this seems like it would apply to the whole tree, not a specific node.

Is there something I'm overlooking, or a different / better way to do this?

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
Brendan Loyot
  • 31
  • 1
  • 2

4 Answers4

2

use the TreePanel's selection model:

treePanel.getSelectionModel().addSelectionChangedListener(
        new SelectionChangedListener<BaseTreeModel>() {

            @Override
            public void selectionChanged(SelectionChangedEvent<BaseTreeModel> se) {

                BaseTreeModel selectedItem = se.getSelectedItem();

                // implement functionality
            }
        }
);

see the TreePanel API for a reference.

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
  • Thanks for the response Eliran. Unless I'm mistaken, TreePanel is from GXT 2 and no longer exists in GXT3, as they changed how the trees worked. Either way, I realized just now that there is a cell action tree implementation in the GXT explorer that can be used for what I need. – Brendan Loyot May 25 '12 at 15:08
  • oh, i didn't notice you're referring to version 3. anyhow please add the solution as an answer to your question so others may benefit from it, thanks. – Eliran Malka May 25 '12 at 15:47
2

Use this for Single Selection

    tree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
    tree.getSelectionModel().addSelectionHandler(new SelectionHandler<MenuView.MenuDto>() {

        public void onSelection(SelectionEvent<MenuDto> event) {
            MenuDto mnu = event.getSelectedItem();
            Info.display("Tree Handler", mnu.getDescripcion());
        }
    });

For Multiple Selections

    tree.getSelectionModel().addSelectionChangedHandler(new SelectionChangedHandler<MenuView.MenuDto>() {

        public void onSelectionChanged(SelectionChangedEvent<MenuDto> event) {
            List<MenuDto> mnus = event.getSelection();
            Info.display("Tree Handler", mnus.get(0).getDescripcion());
        }
    });
Juan Rojas
  • 8,711
  • 1
  • 22
  • 30
2

Another option is to override Tree's onDoubleClick (or onClick) method:

Tree tree = new Tree<MyModel, String>(store, valueProvider){            
    @Override
    protected void onDoubleClick(Event event) {
        TreeNode<MyModel> node = findNode(event.getEventTarget().<Element> cast());
        Info.display("Double Click", "You double clicked this node!");
        super.onDoubleClick(event);
   }
};
Leah Buckley
  • 58
  • 1
  • 6
0

Figured it out.This can be achieved by using the Cell Action Tree, an implementation of which can be found here: http://www.sencha.com/examples/#ExamplePlace:cellactiontree

Brendan Loyot
  • 31
  • 1
  • 2