PDA

View Full Version : TreeItem OnClick Event not firing



goeritz
18 Jun 2008, 4:45 AM
Hi,

currently I'm playing around with GXT and created a test module. I have a BorderLayout with an Accordion in the west region and and a TabPanel in the center region. In the Accordion I have a Tree with some TreeItem and now I'd like to create a new TabItem in the center region when a TreeItem is clicked.

My problem is that the OnClick event doesn't get fired. It would be great if somebody could tell me what I'm doing wrong - Thank you!!

GXT-version: 1.0-beta5
Firefox 2.0.0.14



public class MyTestModule implements EntryPoint {

private TabPanel tabs = null;

public void onModuleLoad() {

Viewport viewport = new Viewport();
viewport.setLayout(new FitLayout());

LayoutContainer layoutContainer = new LayoutContainer();
layoutContainer.setLayout(new BorderLayout());

ContentPanel west = new ContentPanel();
ContentPanel center = new ContentPanel();

BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 200);
westData.setSplit(true);
westData.setCollapsible(true);
westData.setMargins(new Margins(5));

west.setLayout(new AccordionLayout());
west.setHeading("Accordion Layout");

ContentPanel cp = new ContentPanel();
cp.setHeading("Test Widgets");
cp.setScrollMode(Scroll.AUTO);
west.add(cp);

Tree tree = new Tree();
TreeItem family = new TreeItem("Panels");
tree.getRootItem().add(family);

TreeItem item = new TreeItem("FormPanel");
item.addListener(Events.OnClick, new Listener<TreeEvent>() {

public void handleEvent(TreeEvent be) {
TabItem form = new TabItem();
form.setText("FormPanel");
//form.add(getFormPanel());
this.tabs.add(form);

Info.display("TreeEvent fired", "TreeEvent fired");
}
});

family.add(item);
family.setExpanded(true);
cp.add(tree);

cp = new ContentPanel();
cp.setHeading("Settings");
west.add(cp);

cp = new ContentPanel();
cp.setHeading("Misc");
west.add(cp);

BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
centerData.setMargins(new Margins(5, 0, 5, 0));

tabs = new TabPanel();

TabItem home = new TabItem();
home.setText("Home");
tabs.add(home);

center.add(tabs);

layoutContainer.add(west, westData);
layoutContainer.add(center, centerData);

viewport.add(layoutContainer);
RootPanel.get().add(viewport);


}
Regards, Robert

zaccret
18 Jun 2008, 9:47 AM
Try with

item.addListener(Events.SelectionChange, new Listener<TreeEvent>() {instead of

item.addListener(Events.OnClick, new Listener<TreeEvent>() {I think it will also catch keyboard events. Is that what you want to ?

goeritz
18 Jun 2008, 11:33 PM
I have just tried your suggestion but there's still no event fired. I also tried to assign the listener to the parent TreeItem (familiy) but anyhow it doesn't work.
Regarding your question zaccret: At the moment I don't care to catch keyboard events, but thank you for the hint!

So can anyone give me another advice how I could solve my problem? Maybe darrell?

zaccret
19 Jun 2008, 12:54 AM
Sorry for mistake : you have to add the listener to the tree, not the item. Generally speaking, you can see all events handled by a component looking at its javadoc. On TreeItem, you can see BeforeAdd, BeforeRemove... and all those handled by the superclass (Component)
Then you can use tree.getSelectedItem() to look which item is selected.

goeritz
19 Jun 2008, 2:23 AM
Thank you for this good explanation, now it works!!

jpnet
18 Sep 2008, 1:25 PM
How can you do this without the SelectionChange event? I would like an event to be fire when a TreeItem is Clicked, not just when a selection changes. Is there a way to do this??

Thanks,

JP

Cypher
18 Sep 2008, 1:49 PM
I'm having the same problem - SelectionChange listener is not sufficiet. Once the tree item is selected and user clicks on it again, no SelectionChange event is fired...

jpnet
18 Sep 2008, 1:59 PM
Ok... this works:



yourTree.addListener(Events.OnClick, new Listener<TreeEvent>(){
public void handleEvent(TreeEvent be)
{

Window.alert(be.item.getText());
}
});