1 package com.ca.commons.cbutil; 2 3 import javax.swing.*; 4 import java.util.Vector ; 5 6 7 12 13 public class CBJComboBox extends JComboBox 14 { 15 16 20 21 public CBJComboBox() 22 { 23 super(); 24 setKeySelectionManager(new CBKeySelectionManager(this)); 25 } 26 27 28 32 33 public CBJComboBox(ComboBoxModel aModel) 34 { 35 super(aModel); 36 setKeySelectionManager(new CBKeySelectionManager(this)); 37 } 38 39 40 44 45 public CBJComboBox(Object [] items) 46 { 47 super(items); 48 setKeySelectionManager(new CBKeySelectionManager(this)); 49 } 50 51 52 56 57 public CBJComboBox(Vector items) 58 { 59 super(items); 60 setKeySelectionManager(new CBKeySelectionManager(this)); 61 } 62 63 64 75 76 public class CBKeySelectionManager implements KeySelectionManager 77 { 78 81 82 String oldKey = null; 83 84 87 88 int position = -1; 89 90 93 94 long oldTime = -1; 95 96 99 100 CBJComboBox combo; 101 102 103 109 110 public CBKeySelectionManager(CBJComboBox combo) 111 { 112 super(); 113 this.combo = combo; 114 } 115 116 117 126 127 public int selectionForKey(char aKey, ComboBoxModel aModel) 128 { 129 return selectionForKey(aKey, aModel, System.currentTimeMillis()); 130 } 131 132 133 146 147 public int selectionForKey(char aKey, ComboBoxModel aModel, long time) 148 { 149 String key = null; 151 if (oldTime > -1) 152 { 153 if ((time - oldTime) > 2000) 156 oldKey = null; 157 } 158 159 160 if (oldKey != null) key = oldKey + String.valueOf(aKey); else key = String.valueOf(aKey); 165 key = key.toLowerCase(); 166 167 int count = combo.getItemCount(); 168 169 for (int i = 0; i < count; i++) 170 { 171 173 Object temp = combo.getItemAt(i); 174 175 if ((((String ) temp).toLowerCase()).startsWith(key)) { 177 178 position = i; 179 oldKey = String.valueOf(aKey); oldTime = time; 182 return position; 183 } 184 } 185 186 oldKey = String.valueOf(aKey); oldTime = time; 189 return position; 190 } 191 } 192 } | Popular Tags |