Advertisement
gt_ebuddy

Combo Box Filter - document lisenter

May 1st, 2012
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. import javax.swing.DefaultComboBoxModel;
  5. import javax.swing.JComboBox;
  6. import javax.swing.JFrame;
  7. import javax.swing.JTextField;
  8. import javax.swing.UIManager;
  9. import javax.swing.event.DocumentEvent;
  10. import javax.swing.event.DocumentListener;
  11.  
  12. public class FilterComboBox extends JComboBox {
  13.     private List<String> array;
  14.     JTextField textfield;
  15.  
  16.     public FilterComboBox(List<String> array) {
  17.         super(array.toArray());
  18.         this.array = array;
  19.         this.setEditable(true);
  20.         textfield = (JTextField) this.getEditor().getEditorComponent();
  21. //      textfield.addKeyListener(new KeyAdapter() {
  22. //          public void keyReleased(KeyEvent ke) {
  23. //              SwingUtilities.invokeLater(new Runnable() {
  24. //                  public void run() {
  25. //                      comboFilter(textfield.getText());
  26. //                  }
  27. //              });
  28. //          }
  29. //      });
  30.         textfield.getDocument().addDocumentListener(new DocumentListener() {
  31.             @Override
  32.             public void removeUpdate(DocumentEvent e) {
  33.                 callFIlter();
  34.             }
  35.             @Override
  36.             public void insertUpdate(DocumentEvent e) {
  37.                 callFIlter();
  38.             }
  39.             @Override
  40.             public void changedUpdate(DocumentEvent e) {
  41.             }
  42.         });
  43.  
  44.     }
  45.  
  46.     public void callFIlter() {
  47.         try {
  48.             String text = textfield.getDocument().getText(0, textfield.getDocument().getLength());
  49.             comboFilter(text);
  50.         } catch (Exception e2) {
  51.             e2.printStackTrace();
  52.         }
  53.     }
  54.  
  55.     public synchronized void comboFilter(String enteredText) {
  56.         List<String> autoCompleteArray = new ArrayList<String>();
  57.         for (int i = 0; i < array.size(); i++) {
  58.             if (array.get(i).toLowerCase().contains(enteredText.toLowerCase())) {
  59.                 autoCompleteArray.add(array.get(i));
  60.             }
  61.         }
  62.         if (autoCompleteArray.size() > 0) {
  63.             this.setModel(new DefaultComboBoxModel(autoCompleteArray.toArray()));
  64.             this.setSelectedItem(enteredText);
  65.             this.showPopup();
  66.         } else {
  67.             this.hidePopup();
  68.         }
  69.     }
  70.  
  71.     /* Testing Codes */
  72.     public static List<String> populateArray() {
  73.         List<String> test = new ArrayList<String>();
  74.         test.add("");
  75.         test.add("Mountain Flight");
  76.         test.add("Mount Climbing");
  77.         test.add("Trekking");
  78.         test.add("Rafting");
  79.         test.add("Jungle Safari");
  80.         test.add("Bungie Jumping");
  81.         test.add("Para Gliding");
  82.         return test;
  83.     }
  84.     public static void makeUI() {
  85.         JFrame frame = new JFrame("Adventure in Nepal - Combo Test");
  86.  
  87.         frame.getContentPane().add(new FilterComboBox(populateArray()));
  88.         frame.pack();
  89.         frame.setLocationByPlatform(true);
  90.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  91.         frame.setVisible(true);
  92.     }
  93.  
  94.     public static void main(String[] args) throws Exception {
  95.          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  96.         makeUI();
  97.  
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement