Anda pasti pernah (kalau bukan sering) mencari informasi melalui google bukan? Ketika Anda mengetik sesuatu, belum selesai ketik, Google biasanya memberikan beberapa pilihan, seakan akan menebak apa yang ingin Anda ketik, bukan demikian? Hal tersebut dinamakan AutoSuggest, atau memberikan saran secara otomatis.
Post kali ini, saya akan memberikan demo AutoSuggest, tapi bukan untuk web/HTML, melainkan pada aplikasi Desktop dengan Java, menggunakan framework Swing. Demo-nya dapat dilihat di video di bawah ini,
Dan berikut cuplikan source code dari aplikasi di Video tersebut, yang juga merupakan class utama AutoSuggest tersebut.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | package com.kurungkurawal.autosuggest.core; import javax.swing.*; import java.awt.*; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; /** * Created with IntelliJ IDEA. * Author: Konglie */ // autosuggest ini sendiri adalah JComboBox public class AutoSuggest extends JComboBox { private AutoSuggestListener listener = null; private Object LockSelectionTo = null; private JTextField tf = (JTextField) getEditor().getEditorComponent(); private JComponent neighbourFocus = null; public AutoSuggest() { super(); setDefaultListener(); init(); } public AutoSuggest(AutoSuggestListener l) { super(); setListener(l); init(); } public void setListener(AutoSuggestListener l) { this.listener = l; } public AutoSuggestListener getListener() { return this.listener; } private boolean shouldHide = false; private void init() { setEditable(true); // kuncinya adalah memasang event ketika user mengetik di dalamnya // kemudian menjalankan interface AutoSuggestListener tf.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent keyEvent) { int kcode = keyEvent.getKeyCode(); if (kcode == KeyEvent.VK_UP || kcode == KeyEvent.VK_DOWN) { return; } if (kcode == KeyEvent.VK_ESCAPE || kcode == KeyEvent.VK_ENTER) { if (kcode == KeyEvent.VK_ENTER) { if (getItemCount() > 0) { try {if(neighbourFocus != null){ neighbourFocus.grabFocus(); neighbourFocus.requestFocus(); } } catch (Exception e){ e.printStackTrace(); } } } shouldHide = true; if (defaultValue != null && kcode == KeyEvent.VK_ESCAPE) { setValue(defaultValue); } return; } shouldHide = false; EventQueue.invokeLater(new Runnable() { @Override public void run() { String s = tf.getText(); // disinilah proses filter data terjadi // listener ini harus disediakan oleh penyedia data listener.filter(AutoSuggest.this, s); setSuggestModel(s); } }); } }); tf.addFocusListener(new FocusAdapter() { @Override public void focusGained(FocusEvent e) { tf.selectAll(); } }); } public void setLockSelectionTo(Object o) { LockSelectionTo = o; if (LockSelectionTo != null) { setEditable(false); removeAllItems(); setValue(o); } else { setEditable(true); } } public void disableSelection(String text) { setLockSelectionTo(new SimpleKeyValue("", text)); } private void setSuggestModel(String s) { setSelectedIndex(-1); hidePopup(); if (!shouldHide && s.length() > 0 && getItemCount() > 0) { showPopup(); } tf.setText(s); } public Object getValue() { if (this.getSelectedIndex() < 0) { return ""; } return listener.getSelectedObject(getSelectedItem()); } private Object defaultValue = null; public void setValue(Object c) { removeAllItems(); addItem(c); setSelectedIndex(0); defaultValue = c; } private void setDefaultListener() { setListener(new AutoSuggestListener() { @Override public void filter(AutoSuggest c, String s) { } @Override public Object getSelectedObject(Object item) { return item; } }); } public void setNeighbourFocus(JComponent c){ neighbourFocus = c; } } |
Source Code lengkap video di atas dapat didownload di GitHub.