Roughian Examples Site Map - GWT Examples - Tutorials

ListBox


Version 1.0 onwards

The ListBox widget lets you display lists of options



Listeners



Notes


The ListBox widget displays lists of available options as either a dropdown list, or a scrollable list showing a number of items at a time. If you set the visibleItemCount to more than 1 then you will get a scrollable list. Of course, it is only scrollable if there are more items than will fit in the box.

You can get the index of the selected item with getSelectedIndex() and the text of any item with getItemText(index) and obviously you can get the text of the selected item by combining the two.

When you create a new item in the list, you can, optionally, supply a second parameter (the first is the text to be displayed), and this second parameter can be retrieved later with getValue(index). For example, you could display the country name but want to retrieve the country code, dislay the user name, but want the userid.


Code


public void demo()
{
    VerticalPanel panel = new VerticalPanel();
    panel.addStyleName("demo-panel-padded");
    panel.setSize("200px", "120px");
    panel.add(getListBox(false));
    panel.add(getListBox(true));
    RootPanel.get("demo").add(panel);
}
ListBox getListBox(boolean dropdown)
{
    ListBox widget = new ListBox();
    widget.addStyleName("demo-ListBox");
    widget.addItem("One");
    widget.addItem("Two");
    widget.addItem("Three");
    widget.addItem("Four");
    widget.addItem("Five");
    if(!dropdown)widget.setVisibleItemCount(3);
    return widget;
}