Home » Enterprise Java » gwt » ListBox Example

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

ListBox Example

In this example we shall show you how to create a ListBox example 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 BoxList is a widget that presents a list of choices to the user, either as a list box or as a drop-down list. To create a ListBox example we have performed the following steps:

  • The ListBoxExample 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.
  • Create a new Horizontal panel.
  • Create a ListBox that is a Drop Down List. Add Items to the ListBox.
  • Create a new ListBox that displays categories contents. Add Items to this ListBox too.
  • Add a ChangeHandler to dropDownList. Override its onChange(ChangeEvent event) method to handle the change events fire.
  • Add the boxLists to the HorizontalPanel.
  • Add the HorizontalPanel to the RootPanel, that is the panel to which all other widgets must ultimately be added,

as described in the code snippet below. 

package com.javacodegeeks.snippets.enterprise;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;

public class ListBoxExample implements EntryPoint {
    // Constants
	final String[] Items = { "Item 0", "Item 1", "Item 2" };
	final String[] Item0 = { "Item 0,0", "Item 0,1", "Item 0,2" };
	final String[] Item1 = { "Item 1,0", "Item 1,1", "Item 1,2" };
	final String[] Item2 = { "Item 2,0", "Item 2,1", "Item 2,2" };

	public void onModuleLoad() {

  // Create new Horizontal panel
		HorizontalPanel hp = new HorizontalPanel();

  // Create a ListBox - Drop Down List
		final ListBox dropDownList = new ListBox();


  // Add Items' categories
		for (int i = 0; i < Items.length; i++) {

  // Create new ListBox that displays categories contents

  final ListBox contentList = new ListBox();

  // Initialize the List with data from Item0
		for (int i = 0; i < Item0.length; i++) {

  // Add ChangeHandler to dropDownList 

  dropDownList.addChangeHandler(new ChangeHandler() {

			public void onChange(ChangeEvent event) {

    // Get the index of the selected Item

    int Item = dropDownList.getSelectedIndex();

    String[] listData = null;

    // Clear the content list

    // Set content 
				switch (Item) {
				case 0:
					listData = Item0;
				case 1:
					listData = Item1;
				case 2:
					listData = Item2;
				for (int i = 0; i < listData.length; i++) {


  // Add widgets to Vertical Panel 

  // Add Vertical Panel to Root Panel

This was an example of how to create a ListBox example.

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

I have read and agree to the terms & conditions


Notify of

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

Inline Feedbacks
View all comments