Transpiled version (5065L) is out of date.
1 | // TODO: improve filtering |
2 | |
3 | sclass AutoComboBox extends JComboBox<S> {
|
4 | String keyWord[]; |
5 | new Vector myVector; |
6 | bool acceptOnTab; // don't work so good |
7 | |
8 | *() {
|
9 | setModel(new DefaultComboBoxModel(myVector)); |
10 | setSelectedIndex(-1); |
11 | setEditable(true); |
12 | JTextField text = (JTextField) this.getEditor().getEditorComponent(); |
13 | text.setFocusable(true); |
14 | text.setText("");
|
15 | text.addKeyListener(new ComboListener(this, myVector)); |
16 | if (acceptOnTab) text.setFocusTraversalKeysEnabled(false); |
17 | setMyVector(); |
18 | } |
19 | |
20 | /** |
21 | * set the item list of the AutoComboBox |
22 | * @param keyWord an String array |
23 | */ |
24 | void setKeyWord(String[] keyWord) {
|
25 | this.keyWord = keyWord; |
26 | setMyVector(); |
27 | } |
28 | |
29 | void setKeyWord(Collection<S> keyWord) {
|
30 | setKeyWord(toStringArray(keyWord)); |
31 | } |
32 | |
33 | private void setMyVector() {
|
34 | copyArrayToVector(keyWord, myVector); |
35 | } |
36 | |
37 | class ComboListener extends KeyAdapter {
|
38 | JComboBox cb; |
39 | Vector vector; |
40 | |
41 | *(JComboBox *cb, Vector *vector) {}
|
42 | |
43 | public void /*keyReleased*/keyPressed(KeyEvent key) {
|
44 | if (key.getKeyCode() == KeyEvent.VK_ENTER) ret; |
45 | |
46 | if (key.getKeyCode() == KeyEvent.VK_ESCAPE) { cb.hidePopup(); ret; }
|
47 | |
48 | if (acceptOnTab && key.getKeyCode() == KeyEvent.VK_TAB /*&& key.getModifiers() == 0*/) {
|
49 | _print("Have tab event (modifiers=" + key.getModifiers() + ")");
|
50 | if ((key.getModifiers() & ActionEvent.SHIFT_MASK) == 0 && cb.isPopupVisible()) {
|
51 | cb.setSelectedIndex(0); // accept item |
52 | cb.hidePopup(); |
53 | } else // standard tab behavior |
54 | swing_standardTabBehavior(key); |
55 | |
56 | ret; |
57 | } |
58 | |
59 | JTextField tf = cast cb.getEditor().getEditorComponent(); |
60 | |
61 | if (tf.getCaretPosition() != l(tf.getText())) ret; |
62 | |
63 | String text = ((JTextField) key.getSource()).getText(); |
64 | |
65 | Vector list = getFilteredList(text); |
66 | if (nempty(list)) {
|
67 | cb.setModel(new DefaultComboBoxModel(list)); |
68 | cb.setSelectedIndex(-1); |
69 | tf.setText(text); // necessary? |
70 | cb.showPopup(); |
71 | } else |
72 | cb.hidePopup(); |
73 | } |
74 | |
75 | public Vector getFilteredList(String text) {
|
76 | ret emptyAfterTrim(text) ? vector : new Vector(scoredSearch(text, vector)); |
77 | } |
78 | } |
79 | } |
by Malith, http://stackoverflow.com/questions/13681977/jcombobox-autocomplete, modified by Stefan
download show line numbers debug dex old transpilations
Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1007529 |
| Snippet name: | AutoComboBox - uses scoredSearch to find items |
| Eternal ID of this version: | #1007529/35 |
| Text MD5: | e4420a052a35bed7abdf011a63f8f56e |
| Author: | stefan |
| Category: | javax / gui |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-01-24 15:27:12 |
| Source code size: | 2362 bytes / 79 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 1298 / 3869 |
| Version history: | 34 change(s) |
| Referenced in: | [show references] |