gwt

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. 

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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" };
 
    @Override
    public void onModuleLoad() {
 
  // Create new Horizontal panel
        HorizontalPanel hp = new HorizontalPanel();
        hp.setSpacing(20);
 
 
  // Create a ListBox - Drop Down List
        final ListBox dropDownList = new ListBox();
 
   
 
  // Add Items' categories
        for (int i = 0; i < Items.length; i++) {
            dropDownList.addItem(Items[i]);
        }
 
 
  // Create new ListBox that displays categories contents
 
  final ListBox contentList = new ListBox();
        contentList.setVisibleItemCount(5);
 
 
  // Initialize the List with data from Item0
        for (int i = 0; i < Item0.length; i++) {
            contentList.addItem(Item0[i]);
        }
 
 
  // Add ChangeHandler to dropDownList
 
  dropDownList.addChangeHandler(new ChangeHandler() {
 
            @Override
            public void onChange(ChangeEvent event) {
 
 
    // Get the index of the selected Item
 
 
    int Item = dropDownList.getSelectedIndex();
 
 
    String[] listData = null;
 
 
    // Clear the content list
                contentList.clear();
 
 
 
    // Set content
                switch (Item) {
                case 0:
                    listData = Item0;
                    break;
                case 1:
                    listData = Item1;
                    break;
                case 2:
                    listData = Item2;
                    break;
                }
                for (int i = 0; i < listData.length; i++) {
                    contentList.addItem(listData[i]);
                }
            }
        });
 
   
 
  // Add widgets to Vertical Panel
        hp.add(dropDownList);
        hp.add(contentList);
 
 
  // Add Vertical Panel to Root Panel
        RootPanel.get().add(hp);
    }
}

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

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.
Subscribe
Notify of
guest


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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button