PDA

View Full Version : [FIXED] Tree Click events are fired twice



stephen_mcleod
21 Apr 2008, 9:10 PM
If I add a Click event listener to a Tree, it appears that the click events are fired twice for each tree item clicked. eg


public final void onModuleLoad() {

Tree tree = new Tree();
tree.addListener(Events.Click, new Listener<TreeEvent>() {
public void handleEvent(TreeEvent te) {
System.out.println("Click: " + te);
new RuntimeException().printStackTrace();
}
});
tree.getRootItem().add(new TreeItem("Item 1"));
tree.getRootItem().add(new TreeItem("Item 2"));

RootPanel.get().add(tree);
}
Run the above, click on either item, and you can see the event is fired twice. From the stack trace, the event is fired from the following two places:

...
at com.extjs.gxt.ui.client.util.EventTable.sendEvent(EventTable.java:85)
at com.extjs.gxt.ui.client.util.Observable.fireEvent(Observable.java:87)
at com.extjs.gxt.ui.client.widget.Component.fireEvent(Component.java:434)
at com.extjs.gxt.ui.client.widget.tree.TreeItem.onComponentEvent(TreeItem.java:379)
at com.extjs.gxt.ui.client.widget.Component.onBrowserEvent(Component.java:653)
at com.extjs.gxt.ui.client.widget.tree.Tree.onBrowserEvent(Tree.java:474)

...
at com.extjs.gxt.ui.client.util.EventTable.sendEvent(EventTable.java:85)
at com.extjs.gxt.ui.client.util.Observable.fireEvent(Observable.java:87)
at com.extjs.gxt.ui.client.widget.Component.fireEvent(Component.java:434)
at com.extjs.gxt.ui.client.widget.tree.TreeItemUI$1.handleEvent(TreeItemUI.java:35)
at com.extjs.gxt.ui.client.widget.tree.TreeItemUI$1.handleEvent(TreeItemUI.java:1)
at com.extjs.gxt.ui.client.widget.tree.TreeItemUI$3.handleEvent(TreeItemUI.java:69)
at com.extjs.gxt.ui.client.widget.tree.TreeItemUI$3.handleEvent(TreeItemUI.java:1)
at com.extjs.gxt.ui.client.widget.tree.TreeItem.onComponentEvent(TreeItem.java:386)

I'm using gxt-1.0-beta1.

darrellmeyer
23 Apr 2008, 12:02 AM
Yes, you are correct. I am in the process of updating the event code for tree and tree table. In the code prior to 1.0, GXT event types shared the same value as the GWT events. For example:

Events.Click = Event.ONCLICK;

This is no longer true. GXT events and GWT events are separated. GXT events (Events.*) will only be fired by components that declare and fire the event, while GWT events (Event.*) will correspond to dom events that get fired by the browser.

Events.Click is going to be removed. Components use Select and SelectionChange events. You will still be able to listen for dom click events by adding a listener using Event.ONCLICK.

The new code will go in beta2.

darrellmeyer
29 Apr 2008, 7:54 PM
The code works as intended in beta2. You can listen for the native DOM event with Events.OnClick. It only fires once per click. Or you can listen for the SelectionChange event, which is an application event, that fires when the selection has changed.

sz_146
3 Mar 2009, 7:55 AM
Is it not possible to listen for both Events.OnClick and Events.SelectionChange.

I need the OnClick for making the tree expand on single click and need selection change for creating necessary tabs etc. I tried :




NavTreeListener listener = new NavTreeListener();
tree.addListener(Events.SelectionChange, listener);
tree.addListener(Events.OnClick, listener);


and



public class NavTreeListener implements Listener<TreeEvent>{

public void handleEvent(TreeEvent event) {

if( event.getEventType() == Events.OnClick ){

final TreeItem t = event.item;

if(!t.isLeaf()){
if(!t.isExpanded()){
t.setExpanded(true);
}else{
t.setExpanded(false);
}
}

}

if(event.getEventType() == Events.SelectionChange){

TreeItem t = event.selected.get(0);
MainBodyTabPanel.getInstance().addTab(t.getText());

}
}

}


But the SelectionChange never fires.

sven
3 Mar 2009, 8:01 AM
tree.addListener(Events.SelectionChange, new Listener<TreeEvent>() {
public void handleEvent(TreeEvent be) {
System.out.println("selectionchange");
}
});
tree.addListener(Events.OnClick, new Listener<TreeEvent>() {
public void handleEvent(TreeEvent be) {
System.out.println("onclick");
}
});

Works fine for me.

sven
3 Mar 2009, 8:02 AM
getEventType

return the dom event type. This wont work for you.

sz_146
3 Mar 2009, 8:56 AM
Thanks. I was trying to implement listeners in a separate class. I am now using :



tree.addListener(Events.SelectionChange, new Listener<TreeEvent>(){
public void handleEvent(TreeEvent event) {
TreeItem t = event.selected.get(0);
t.toggle();
MainBodyTabPanel.getInstance().addTab(t.getText());
}
});


and it solves the issue of expanding on single click as well doing other stuff. However I would still like to put the listeners in a separate class.

sven
3 Mar 2009, 9:01 AM
I think the help forum is a better place for your questions.

sz_146
3 Mar 2009, 9:04 AM
Yes I will thanks.