-
-
Save malithjkmt/5e0c5d7880142df8face to your computer and use it in GitHub Desktop.
ComboBox AutoComplete Java Swing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Vector; | |
import javax.swing.DefaultComboBoxModel; | |
import javax.swing.JComboBox; | |
import javax.swing.JTextField; | |
public class AutoComboBox extends JComboBox<Object> { | |
// default item list | |
String keyWord[] = {"item1", "item2", "item3"}; | |
Vector myVector = new Vector(); | |
public AutoComboBox() { | |
setModel(new DefaultComboBoxModel(myVector)); | |
setSelectedIndex(-1); | |
setEditable(true); | |
JTextField text = (JTextField) this.getEditor().getEditorComponent(); | |
text.setFocusable(true); | |
text.setText(""); | |
text.addKeyListener(new ComboListener(this, myVector)); | |
setMyVector(); | |
} | |
/** | |
* set the item list of the AutoComboBox | |
* @param keyWord An String array | |
*/ | |
public void setKeyWord(String[] keyWord) { | |
this.keyWord = keyWord; | |
setMyVectorInitial(); | |
} | |
private void setMyVector() { | |
int a; | |
for (a = 0; a < keyWord.length; a++) { | |
myVector.add(keyWord[a]); | |
} | |
} | |
private void setMyVectorInitial() { | |
myVector.clear(); | |
int a; | |
for (a = 0; a < keyWord.length; a++) { | |
myVector.add(keyWord[a]); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event.KeyAdapter; | |
import java.awt.event.KeyEvent; | |
import java.util.Locale; | |
import java.util.Vector; | |
import javax.swing.DefaultComboBoxModel; | |
import javax.swing.JComboBox; | |
import javax.swing.JTextField; | |
import org.apache.commons.lang3.text.WordUtils; | |
public class ComboListener extends KeyAdapter | |
{ | |
@SuppressWarnings("rawtypes") | |
JComboBox cbListener; | |
@SuppressWarnings("rawtypes") | |
Vector vector; | |
@SuppressWarnings("rawtypes") | |
public ComboListener(JComboBox cbListenerParam, Vector vectorParam) | |
{ | |
cbListener = cbListenerParam; | |
vector = vectorParam; | |
} | |
@SuppressWarnings({ "unchecked", "rawtypes" }) | |
public void keyReleased(KeyEvent key) | |
{ | |
// TODO Auto-generated method stub | |
String text = ((JTextField)key.getSource()).getText(); | |
cbListener.setModel(new DefaultComboBoxModel(getFilteredList(text))); | |
cbListener.setSelectedIndex(-1); | |
((JTextField)cbListener.getEditor().getEditorComponent()).setText(text); | |
cbListener.showPopup(); | |
} | |
@SuppressWarnings({ "rawtypes", "unchecked" }) | |
public Vector getFilteredList(String text) | |
{ | |
Vector v = new Vector(); | |
for(int a = 0;a<vector.size();a++) | |
{ | |
if(vector.get(a).toString().startsWith(text)) | |
{ | |
v.add(vector.get(a).toString()); | |
} | |
else if(vector.get(a).toString().startsWith(text.toLowerCase())) | |
{ | |
v.add(vector.get(a).toString()); | |
} | |
else if(vector.get(a).toString().startsWith(text.toUpperCase())) | |
{ | |
v.add(vector.get(a).toString()); | |
} | |
else if(vector.get(a).toString().startsWith(WordUtils.capitalizeFully(text))) | |
{ | |
v.add(vector.get(a).toString()); | |
} | |
else if(vector.get(a).toString().startsWith(WordUtils.uncapitalize(text))) | |
{ | |
v.add(vector.get(a).toString()); | |
} | |
} | |
return v; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Though the typing text is upper Case, lowercase or Title case the result will loaded.