TreePanel is another powerful UI tools provide by GXT. It has expand-collapse functionality. You can add context menu or can add filter functionality with the TreePanel very easily. Here i describe every steps of creating a simple basic Tree using GXT in detail.
First create a new project named GxtBasicTree and add GXT library in the project. The tree node objects must possess the functionality of BaseTreeModel of GXT. In my example the leaf node objects are of EmployeeTreeNode class and parent node objects are of Folder class. Both of the classes extend BaseTreeModel class. Here is the code for the Employee class.
package com.ratul.GxtBasicTree.client.model;
import java.util.Date;
import com.extjs.gxt.ui.client.data.BaseTreeModel;
public class EmployeeTreeNode extends BaseTreeModel {
private static final long serialVersionUID = 1L;
public EmployeeTreeNode() {
}
public EmployeeTreeNode(String name, double salary, Date joiningdate) {
set("name", name);
set("salary", salary);
set("joiningdate", joiningdate);
}
public Date getJoiningdate() {
return (Date) get("joiningdate");
}
public String getName() {
return (String) get("name");
}
public double getSalary() {
Double salary = (Double) get("salary");
return salary.doubleValue();
}
public String toString() {
return getName();
}
}
As i have said previously the objects of the Folder class can be parent node for the Tree. So this class should have the functionality of adding children nodes. The constructor with a String and an Array of BaseTreeModel object serves the purpose. Here is the code for the Folder class.
package com.ratul.GxtBasicTree.client.model;
import java.io.Serializable;
import com.extjs.gxt.ui.client.data.BaseTreeModel;
public class Folder extends BaseTreeModel implements Serializable {
private static final long serialVersionUID = 1L;
private static int ID = 0;
public Folder() {
set("id", ID++);
}
public Folder(String name) {
set("id", ID++);
set("name", name);
}
public Folder(String name, BaseTreeModel[] children) {
this(name);
for (int i = 0; i < children.length; i++) {
add(children[i]);
}
}
public Integer getId() {
return (Integer) get("id");
}
public String getName() {
return (String) get("name");
}
public String toString() {
return getName();
}
}
Now populate a root node containing all the child nodes by using the above two classes in the
getTreeModel methodh of the
TestData class.
package com.ratul.GxtBasicTree.client;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.ratul.GxtBasicTree.client.model.Employee;
import com.ratul.GxtBasicTree.client.model.Folder;
public class TestData {
public static Folder getTreeModel()
{
DateTimeFormat f = DateTimeFormat.getFormat("yyyy-mm-dd");
Folder[] folders = new Folder[] {
new Folder("General Administration", new Folder[] {
new Folder("General Manager", new EmployeeTreeNode[] {
new EmployeeTreeNode("Hollie Voss", 150000, f.parse("2006-05-01")),
new EmployeeTreeNode("Heriberto Rush", 150000,f.parse("2007-08-01")), }),
new Folder("Executive", new EmployeeTreeNode[] {
new EmployeeTreeNode("Christina Blake", 45000,f.parse("2008-11-01")),
new EmployeeTreeNode("Chad Andrews", 45000, f.parse("2008-07-01")), }), }),
new Folder("Information Technology",new Folder[] {
new Folder("Senior S/W Engineer",new EmployeeTreeNode[] {
new EmployeeTreeNode("Dirk Newman", 70000,f.parse("2007-08-21")),
new EmployeeTreeNode("Emerson Milton",72000,f.parse("2009-05-07")),
new EmployeeTreeNode("Gail Horton", 680000,f.parse("2008-05-01")), }),
new Folder("S/W Engineer",new EmployeeTreeNode[] {
new EmployeeTreeNode("Claudio Engle", 50000,f.parse("2007-02-01")),
new EmployeeTreeNode("Buster misjenou",52000,f.parse("2009-06-10")),
new EmployeeTreeNode("Bell Snedden", 50000,f.parse("2008-12-01")),
new EmployeeTreeNode("Benito Meeks", 55000,f.parse("2006-05-01")), }), }),
new Folder("Marketing", new Folder[] {
new Folder("Executive",new EmployeeTreeNode[] {
new EmployeeTreeNode("Candice Carson", 50000, f.parse("2007-08-21")),
new EmployeeTreeNode("Mildred Starnes", 50000,f.parse("2008-05-01")),
new EmployeeTreeNode("Claudio Engle", 50000, f.parse("2009-06-15")), }), }),
};
Folder root = new Folder("root");
for (int i = 0; i < folders.length; i++) {
root.add((Folder) folders[i]);
}
return root;
}
}
You data is now ready to create a simple Tree. Go to the
onModuleLoad method of
GxtBasicTree class. Remove all the auto generated code of this method. Now first create a root object named
model of the Folder class from
TestData.getTreeModel() method and create a TreeStore object,
store from this model.
public static final ExampleIcons ICONS = GWT.create(ExampleIcons.class);
public void onModuleLoad()
{
Folder model = TestData.getTreeModel();
TreeStore<ModelData> store = new TreeStore<ModelData>();
store.add(model.getChildren(), true);
Now create a TreePanel,
tree from this store and set display name, width and style of the tree.
final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
tree.setDisplayProperty("name");
tree.setWidth(250);
tree.getStyle().setLeafIcon(ICONS.user_add());
The two button
expand and
collapse provide the expand and collapse functionality by simple calling the
tree.expandAll() and
tree.collapseAll() method.
ButtonBar buttonBar = new ButtonBar();
Button expand = new Button("Expand All");
Button collapse = new Button("Collapse All");
expand.addSelectionListener(new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
tree.expandAll();
}
});
collapse.addSelectionListener(new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
tree.collapseAll();
}
});
buttonBar.add(expand);
buttonBar.add(collapse);
RootPanel.get().add(buttonBar);
RootPanel.get().add(tree);
}
That's all. Run it and check the functionality of the simple tree. To view the live demo of this tutorial
Click here.