2014-03-25 56 views

回答

3

嘗試任何一個:

覆蓋setSelectedIndex()法和火ChangeEvent

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

OR

覆蓋setSelectedIndex()方法調用,用於ChangeHandler給予同樣的行爲同樣的方法。

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())); 
}