1 29 30 package nextapp.echo2.app; 31 32 import nextapp.echo2.app.list.AbstractListComponent; 33 import nextapp.echo2.app.list.DefaultListModel; 34 import nextapp.echo2.app.list.ListModel; 35 36 39 public class SelectField extends AbstractListComponent { 40 41 44 public SelectField() { 45 this((ListModel) null); 46 } 47 48 53 public SelectField(ListModel model) { 54 super(model, null); 55 } 56 57 64 public SelectField(Object [] items) { 65 this(new DefaultListModel(items)); 66 } 67 68 73 public int getSelectedIndex() { 74 return getSelectionModel().getMinSelectedIndex(); 75 } 76 77 82 public Object getSelectedItem() { 83 int selectedIndex = getSelectionModel().getMinSelectedIndex(); 84 return selectedIndex == -1 ? null : getModel().get(selectedIndex); 85 } 86 87 92 public void setSelectedIndex(int index) { 93 if (index == -1) { 94 getSelectionModel().clearSelection(); 95 } else { 96 getSelectionModel().setSelectedIndex(index, true); 97 } 98 } 99 100 105 public void setSelectedItem(Object item) { 106 if (item != null) { 107 ListModel model = getModel(); 108 int size = model.size(); 109 for (int i = 0; i < size; i++) { 110 if (item.equals(model.get(i))) { 111 setSelectedIndex(i); 112 return; 113 } 114 } 115 } 116 setSelectedIndex(-1); 117 } 118 } 119 | Popular Tags |