Home » Enterprise Java » gwt » StaticTree Example

About Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

StaticTree Example

With this example we are going to demonstrate how to create a static Tree, using the Google Web Toolkit, that is an open source set of tools that allows web developers to create and maintain complex JavaScript front-end applications in Java. A Tree is a standard hierarchical tree widget. The tree contains a hierarchy of TreeItems that the user can open, close, and select. In short, to create a static Tree we have followed the steps below:

  • The StaticTreeExample class implements the com.google.gwt.core.client.EntryPoint interface to allow the class to act as a module entry point. It overrides its onModuleLoad() method.
  • We create a new Tree object and add TreeItem objects to the Tree object.
  • We add the Tree to the RootPanel, that is the panel to which all other widgets must ultimately be added.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.enterprise;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

public class StaticTreeExample implements EntryPoint {

	@Override
	public void onModuleLoad() {
		

  // Create new Tree object
		Tree tree = new Tree(); 
		

  // Create new tree Item object
		TreeItem Item0 = new TreeItem("Item 0");
		

  // Add Items to Item0

  Item0.addItem("Item 0.0");
		Item0.addItem("Item 0.1");
		Item0.addItem("Item 0.2");


  // Create new tree object
		TreeItem Item03 = new TreeItem("Item 0.3");
		

  // Add Items to Item 0.3

  Item03.addItem("Item 0.3.0");
		Item03.addItem("Item 0.3.1");
		Item03.addItem(new CheckBox("Item 0.3.2"));
		

  // Add Item 0.3 to Item 0
		Item0.addItem(Item03);
		

  // Create new Item and add TreeItems
		TreeItem Item1 = new TreeItem("Item 1");
		Item1.addItem("Item 1.0");
		Item1.addItem("Item 1.1");
		Item1.addItem("Item 1.2");
		Item1.addItem("Item 1.3");


  // Add TreeItems to tree
		tree.addItem(Item0);
		tree.addItem(Item1);
		

  // Add tree to RootPanel
		RootPanel.get().add(tree);
		
	}
	
}

 
This was an example of how to create a static Tree using the Google Web Toolkit.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments