开发者

Gwt Listbox item reference to an Object

开发者 https://www.devze.com 2023-03-19 17:37 出处:网络
I have got a Listbox and i want to add some Items. There are only methods to add items as a String, but

I have got a Listbox and i want to add some Items. There are only methods to add items as a String, but I want add a Item to the Listbox with a String and a reference to an Object. So that, if a item was selected in the Listbox, I get the O开发者_运维问答bject reference too. Otherwise I have always to search with equal for the right Object.

Is there any option therefore?


Try ValueListBox instead of ListBox.


You could store the object in a Map indexed by the value of the item, or in an array or List, at the index of the added item in the list box.


This is an old question, but since the most elegant answer is not yet here:

// Applicable for an object of specified type 'User'
ValueListBox<User> lbUser = new ValueListBox<User>(new Renderer<User>() {

  public String render(User user) {
    String s = "";
    if (user != null) {
      // Specify the format for the Strings to display per list item here. In this example, it is 
      // 'username (firstname lastname)'
      // For example: MTielemans (Mark Tielemans)
      s = user.getUsername() + "("+user.getFirstname()+" " + user.getLastname()+")";
    } else {
      s = "Select a user";
    }
    return s; 
  }

  public void render(User user, Appendable appendable) throws IOException {
      String s = render(user);
      appendable.append(s);
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号