Search code examples
javasmartgwt

SmartGWT - ordered view of values in comboboxitem


I'm using Java7 and SmartGWT 3.1. Having the following code I encountered that the values are not displayed in the order I put them into.

LinkedHashMap<Integer, String> insertOrdered = new LinkedHashMap<Integer, String>();
for (MyEnum me : MyEnum.values()) {
  insertOrdered.put(me.getId(), me.getMyName());
}
ComboBoxItem cbi = new ComboBoxItem();
cbi.setValueMap(insertOrdered);

the ID's are not ordered (neither ascending nor descending). But, this shouldn't matter, because if I'm not totally mistaken, LinkedHashMap orders with respect to insertion. Just to be sure I checked the JavaDoc again (http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html) and that's the case.

Why is the ComboBoxItem ignoring my ordering? Is there any other way to achieve ordering in a ComboBoxItem?


Solution

  • The Javadoc of LinkedHashMap states:

    This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

    Even though, I would agree that the order that they appear should be the order that they are declared in the enum as stated by JLS. If you are having problems with LinkedHashMap ans you really want to ensure this order, perhaps you might want to try setting the values with the variable argument list method. You will essentially change it to an array, it might be undesired but it could potentially solve your issue.

    List<String> list = new ArrayList<String>();
    for(MyEnum me: MyEnum.values()){
        list.add(me.getMyName());
     }
    
     ComboBoxItem cbi = new ComboBoxItem();
     cbi.setValueMap(list.toArray(new String[list.size()]));