import java.awt.event. * ; import javax.swing. * ; import javax.swing.text. * ; public class ComboBoxPopup extends PlainDocument ... { JComboBox comboBox; ComboBoxModel model; JTextComponent editor; boolean selecting=false; public ComboBoxPopup(final JComboBox comboBox) ...{ this.comboBox = comboBox; model = comboBox.getModel(); editor = (JTextComponent) comboBox.getEditor().getEditorComponent(); comboBox.addActionListener(new ActionListener() ...{ public void actionPerformed(ActionEvent e) ...{ if (!selecting) highlightCompletedText(0); } }); editor.addKeyListener(new KeyAdapter() ...{ public void keyPressed(KeyEvent e) ...{ if (comboBox.isDisplayable()) comboBox.setPopupVisible(true); } }); } public void remove(int offs, int len) throws BadLocationException ...{ if (selecting) return; super.remove(offs, len); } public void insertString(int offs, String str, AttributeSet a) throws BadLocationException ...{ if (selecting) return; super.insertString(offs, str, a); Object item = lookupItem(getText(0, getLength())); if (item != null) ...{ setSelectedItem(item); } else ...{ item = comboBox.getSelectedItem(); offs = offs-str.length(); comboBox.getToolkit().beep(); } setText(item.toString()); highlightCompletedText(offs+str.length()); } private void setText(String text) throws BadLocationException ...{ super.remove(0, getLength()); super.insertString(0, text, null); } private void highlightCompletedText(int start) ...{ editor.setSelectionStart(start); editor.setSelectionEnd(getLength()); } private void setSelectedItem(Object item) ...{ selecting = true; model.setSelectedItem(item); selecting = false; } private Object lookupItem(String pattern) ...{ Object selectedItem = model.getSelectedItem(); if (selectedItem != null && startsWithIgnoreCase(selectedItem.toString(), pattern)) ...{ return selectedItem; } else ...{ for (int i=0, n=model.getSize(); i < n; i++) ...{ Object currentItem = model.getElementAt(i); if (startsWithIgnoreCase(currentItem.toString(), pattern)) ...{ return currentItem; } } } return null; } private boolean startsWithIgnoreCase(String str1, String str2) ...{ return str1.toUpperCase().startsWith(str2.toUpperCase()); } private static void createAndShowGUI() ...{ JComboBox comboBox = new JComboBox(new Object[] ...{"Ester", "Jordi", "Jordina", "Jorge", "Sergi"}); comboBox.setEditable(true); JTextComponent editor = (JTextComponent) comboBox.getEditor().getEditorComponent(); editor.setDocument(new ComboBoxPopup(comboBox)); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(3); frame.getContentPane().add(comboBox); frame.pack(); frame.setVisible(true); } public static void main(String[] args) ...{ javax.swing.SwingUtilities.invokeLater(new Runnable() ...{ public void run() ...{ createAndShowGUI(); } }); }}