package com.intuit.tank.tools.debugger; /* * #%L * Intuit Tank Agent Debugger * %% * Copyright (C) 2011 - 2015 Intuit Inc. * %% * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * #L% */ import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.List; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class FilterComboBox extends JComboBox { private static final long serialVersionUID = 1L; private List<String> array; public FilterComboBox(List<String> array) { super(array.toArray()); this.array = array; this.setEditable(true); final JTextField textfield = (JTextField) this.getEditor().getEditorComponent(); textfield.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent ke) { SwingUtilities.invokeLater(new Runnable() { public void run() { comboFilter(textfield.getText()); } }); } }); } public void comboFilter(String enteredText) { List<String> filterArray = new ArrayList<String>(); for (int i = 0; i < array.size(); i++) { if (array.get(i).toLowerCase().contains(enteredText.toLowerCase())) { filterArray.add(array.get(i)); } } if (filterArray.size() > 0) { this.setModel(new DefaultComboBoxModel(filterArray.toArray())); this.setSelectedItem(enteredText); this.showPopup(); } else { this.hidePopup(); } } /* Testing Codes */ public static List<String> populateArray() { List<String> test = new ArrayList<String>(); test.add(""); test.add("Mountain Flight"); test.add("Mount Climbing"); test.add("Trekking"); test.add("Rafting"); test.add("Jungle Safari"); test.add("Bungie Jumping"); test.add("Para Gliding"); return test; } public static void makeUI() { JFrame frame = new JFrame("Adventure in Nepal - Combo Filter Test"); FilterComboBox acb = new FilterComboBox(populateArray()); frame.getContentPane().add(acb); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) throws Exception { // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); makeUI(); } }