GWT 1.7 using ChangeHandler on ListBox (drop-down)

Here is a simple example of how to listen to the change event.
This functionality replaces the deprecated listBox.addChangeListener(new ChangeListener(){//implementation} );


private ListBox populateLearningLanguage()
{
final ListBox listBox = new ListBox();
addLanguages(listBox);
listBox.setSelectedIndex(2);

listBox.addChangeHandler(new ChangeHandler()
{
public void onChange(ChangeEvent event)
{
int selectedIndex = listBox.getSelectedIndex();
if (selectedIndex > 0)
Window.alert("Something got selected " + listBox.getValue(selectedIndex));
}
});
return listBox;
}

3 comments:

  1. I think your if-statement should be:

    if (selectedIndex>-1) ..

    ReplyDelete
  2. Hi Willem,
    thank you for the correction, yes you are right if the first item is a valid selection and not "please select .."

    ReplyDelete