Java/GWT/Tree
Create Tree With Pending Item
package com.jexp.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.TreeListener;
public class GWTClient implements EntryPoint,TreeListener {
class PendingItem extends TreeItem {
public PendingItem() {
super("Please wait...");
}
}
class TreeNode {
public TreeNode[] children;
public TreeItem item;
public String text;
public TreeNode(String text) {
this.text = text;
}
public TreeNode(String text, TreeNode[] children) {
this(text);
this.children = children;
}
}
TreeNode[] fTreeNode = new TreeNode[]{
new TreeNode("A", new TreeNode[]{
new TreeNode("B", new TreeNode[]{
new TreeNode("1"),
new TreeNode("2"),
new TreeNode("3"),
new TreeNode("4"),
new TreeNode("5"),}),
new TreeNode("B", new TreeNode[]{
new TreeNode("B1"),
new TreeNode("B2"),
new TreeNode("B3"),}),
new TreeNode("C", new TreeNode[]{
new TreeNode("C1"),
new TreeNode("C2"),}),
new TreeNode("D", new TreeNode[]{
new TreeNode("1"),
new TreeNode("2"),
new TreeNode("3"),}),}),
new TreeNode("AA", new TreeNode[]{
new TreeNode("A", new TreeNode[]{
new TreeNode("A1"),
new TreeNode("A2"),
new TreeNode("A3"),
new TreeNode("A4"),}),
new TreeNode("B", new TreeNode[]{
new TreeNode("1"),
new TreeNode("2"),
new TreeNode("3"),
new TreeNode("4"),}),
new TreeNode("C", new TreeNode[]{
new TreeNode("F"),
new TreeNode("E"),}),
new TreeNode("G", new TreeNode[]{
new TreeNode("C"),
new TreeNode("2"),
new TreeNode("F"),
new TreeNode("E"),}),}),
new TreeNode("M", new TreeNode[]{
new TreeNode("C", new TreeNode[]{
new TreeNode("P"),
new TreeNode("V"),}),}),};
public void onModuleLoad() {
Tree tree = new Tree();
for (int i = 0; i < fTreeNode.length; ++i) {
createItem(fTreeNode[i]);
tree.addItem(fTreeNode[i].item);
}
tree.addTreeListener(this);
tree.setWidth("20em");
RootPanel.get().add(tree);
}
private void createItem(TreeNode TreeNode) {
TreeNode.item = new TreeItem(TreeNode.text);
TreeNode.item.setUserObject(TreeNode);
if (TreeNode.children != null) {
TreeNode.item.addItem(new PendingItem());
}
}
public void onTreeItemSelected(TreeItem item) {
Window.alert(((TreeNode) item.getUserObject()).text);
}
public void onTreeItemStateChanged(TreeItem item) {
TreeItem child = item.getChild(0);
if (child instanceof PendingItem) {
item.removeItem(child);
TreeNode tn = (TreeNode) item.getUserObject();
for (int i = 0; i < tn.children.length; ++i) {
createItem(tn.children[i]);
item.addItem(tn.children[i].item);
}
}
}
}
Use TreeListener
package com.jexp.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.TreeListener;
public class GWTClient implements EntryPoint,TreeListener {
class PendingItem extends TreeItem {
public PendingItem() {
super("Please wait...");
}
}
class TreeNode {
public TreeNode[] children;
public TreeItem item;
public String text;
public TreeNode(String text) {
this.text = text;
}
public TreeNode(String text, TreeNode[] children) {
this(text);
this.children = children;
}
}
TreeNode[] fTreeNode = new TreeNode[]{
new TreeNode("A", new TreeNode[]{
new TreeNode("B", new TreeNode[]{
new TreeNode("1"),
new TreeNode("2"),
new TreeNode("3"),
new TreeNode("4"),
new TreeNode("5"),}),
new TreeNode("B", new TreeNode[]{
new TreeNode("B1"),
new TreeNode("B2"),
new TreeNode("B3"),}),
new TreeNode("C", new TreeNode[]{
new TreeNode("C1"),
new TreeNode("C2"),}),
new TreeNode("D", new TreeNode[]{
new TreeNode("1"),
new TreeNode("2"),
new TreeNode("3"),}),}),
new TreeNode("AA", new TreeNode[]{
new TreeNode("A", new TreeNode[]{
new TreeNode("A1"),
new TreeNode("A2"),
new TreeNode("A3"),
new TreeNode("A4"),}),
new TreeNode("B", new TreeNode[]{
new TreeNode("1"),
new TreeNode("2"),
new TreeNode("3"),
new TreeNode("4"),}),
new TreeNode("C", new TreeNode[]{
new TreeNode("F"),
new TreeNode("E"),}),
new TreeNode("G", new TreeNode[]{
new TreeNode("C"),
new TreeNode("2"),
new TreeNode("F"),
new TreeNode("E"),}),}),
new TreeNode("M", new TreeNode[]{
new TreeNode("C", new TreeNode[]{
new TreeNode("P"),
new TreeNode("V"),}),}),};
public void onModuleLoad() {
Tree tree = new Tree();
for (int i = 0; i < fTreeNode.length; ++i) {
createItem(fTreeNode[i]);
tree.addItem(fTreeNode[i].item);
}
tree.addTreeListener(this);
tree.setWidth("20em");
RootPanel.get().add(tree);
}
private void createItem(TreeNode TreeNode) {
TreeNode.item = new TreeItem(TreeNode.text);
TreeNode.item.setUserObject(TreeNode);
if (TreeNode.children != null) {
TreeNode.item.addItem(new PendingItem());
}
}
public void onTreeItemSelected(TreeItem item) {
Window.alert(((TreeNode) item.getUserObject()).text);
}
public void onTreeItemStateChanged(TreeItem item) {
TreeItem child = item.getChild(0);
if (child instanceof PendingItem) {
item.removeItem(child);
TreeNode tn = (TreeNode) item.getUserObject();
for (int i = 0; i < tn.children.length; ++i) {
createItem(tn.children[i]);
item.addItem(tn.children[i].item);
}
}
}
}