Botre Posted April 3, 2016 Share Posted April 3, 2016 (edited) FilteredComboBox Inspiration: http://stackoverflow.com/questions/10368856/jcombobox-filter-in-java-look-and-feel-independent Example: public static void main(String[] args) { JFrame frame = new JFrame("FilteredComboBoxExample"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); JPanel content = new JPanel(); frame.setContentPane(content); FilteredComboBox<Skill> filteredComboBox = new FilteredComboBox<>(Skill.values(), s -> s.toString()); filteredComboBox.setPreferredSize(new Dimension(240, 60)); content.add(filteredComboBox); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } Snippet: Not very MVC friendly but hey this is Swing after all. import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Function; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JTextField; public class FilteredComboBox<T> extends JComboBox<T> { private static final long serialVersionUID = 283907792618669032L; @SuppressWarnings("unused") private List<T> full; private List<T> filtered; @SuppressWarnings("unused") private Function<T, String> fetcher; private JTextField field; @SuppressWarnings({ "unchecked", "rawtypes" }) public FilteredComboBox(List<T> full, Function<T, String> fetcher) { setModel(new DefaultComboBoxModel(full.toArray())); this.full = full; this.fetcher = fetcher; setSelectedIndex(0); setEditable(true); field = (JTextField) getEditor().getEditorComponent(); field.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent k) { String input = field.getText(); int code = k.getKeyCode(); if(!k.isActionKey() && k.getModifiers() == 0 && code != KeyEvent.VK_ENTER && code != KeyEvent.VK_CONTROL && code != KeyEvent.VK_ESCAPE) { filtered = new ArrayList<>(); for (int i = 0; i < full.size(); i++) { if (fetcher.apply(full.get(i)).toLowerCase().contains(input.toLowerCase())) { filtered.add(full.get(i)); } } if (filtered.size() > 0) { setModel(new DefaultComboBoxModel(filtered.toArray())); setSelectedItem(input); showPopup(); } else { setModel(new DefaultComboBoxModel(full.toArray())); setSelectedItem("No result matching: \" " + input + " \""); field.selectAll(); showPopup(); } } } }); } public FilteredComboBox(T[] full, Function<T, String> fetcher) { this(Arrays.asList(full), fetcher); } } Edited April 3, 2016 by Botre 1 Quote Link to comment Share on other sites More sharing options...
Vilius Posted April 3, 2016 Share Posted April 3, 2016 wet snippers, 1 Quote Link to comment Share on other sites More sharing options...
Apaec Posted April 3, 2016 Share Posted April 3, 2016 hey this is awesome! i should totally add this to my cooking / smithing scripts! 1 Quote Link to comment Share on other sites More sharing options...