Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT Listbox OnChangeHandler

Tags:

gwt

How do I explicitly fire the OnChangeHandler for GWT listbox?

In my scenario it should be fired when I call the SetSelectedIndex(X) method of ListBox.

like image 990
Tarish Saini Avatar asked Mar 25 '14 09:03

Tarish Saini


1 Answers

Try any one:

Override setSelectedIndex() method and fire the ChangeEvent

    final ListBox lb = new ListBox() {
        @Override
        public void setSelectedIndex(int index) {
            super.setSelectedIndex(index);
            DomEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
        }
    };

OR

Override setSelectedIndex() method and call the same method that is used for ChangeHandler giving same behavior.

public void onModuleLoad() {

    // Make a new list box, adding a few items to it.
    final ListBox lb = new ListBox() {
        @Override
        public void setSelectedIndex(int index) {
            super.setSelectedIndex(index);
            onChangeBody(this);
        }
    };
    lb.addItem("foo");
    lb.addItem("bar");
    lb.addItem("baz");
    lb.addItem("toto");
    lb.addItem("tintin");

    lb.addChangeHandler(new ChangeHandler() {

        @Override
        public void onChange(ChangeEvent event) {
            onChangeBody(lb);
        }
    });

    // Make enough room for all five items (setting this value to 1 turns it
    // into a drop-down list).
    lb.setVisibleItemCount(5);

    // Add it to the root panel.
    RootPanel.get().add(lb);
}

public void onChangeBody(ListBox lb) {
    System.out.println(lb.getValue(lb.getSelectedIndex()));
}
like image 120
Braj Avatar answered Sep 29 '22 09:09

Braj