Advertisement
gt_ebuddy

Untitled

May 10th, 2012
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. package com.testss;
  2.  
  3. import java.awt.event.KeyAdapter;
  4. import java.awt.event.KeyEvent;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import javax.swing.DefaultComboBoxModel;
  9. import javax.swing.JComboBox;
  10. import javax.swing.JFrame;
  11. import javax.swing.JTextField;
  12. import javax.swing.SwingUtilities;
  13. import javax.swing.UIManager;
  14.  
  15. public class FilterComboBoxStackOverflow extends JComboBox {
  16.     private List<String> array;
  17.  
  18.     public FilterComboBoxStackOverflow(List<String> array) {
  19.         super(array.toArray());
  20.         this.array = array;
  21.         this.setEditable(true);
  22.         final JTextField textfield = (JTextField) this.getEditor().getEditorComponent();
  23.         textfield.addKeyListener(new KeyAdapter() {
  24.             public void keyReleased(KeyEvent ke) {
  25.                 SwingUtilities.invokeLater(new Runnable() {
  26.                     public void run() {
  27.                         comboFilter(textfield.getText());
  28.                     }
  29.                 });
  30.             }
  31.         });
  32.  
  33.     }
  34.  
  35.     public void comboFilter(String enteredText) {
  36.         List<String> filterArray= new ArrayList<String>();
  37.         for (int i = 0; i < array.size(); i++) {
  38.             if (array.get(i).toLowerCase().contains(enteredText.toLowerCase())) {
  39.                 filterArray.add(array.get(i));
  40.             }
  41.         }
  42.         if (filterArray.size() > 0) {
  43.             DefaultComboBoxModel model = (DefaultComboBoxModel) this.getModel();
  44.             model.removeAllElements();
  45.             for (String s: filterArray)
  46.                 model.addElement(s);
  47.  
  48.             JTextField textfield = (JTextField) this.getEditor().getEditorComponent();
  49.             textfield.setText(enteredText);
  50.             //this.showPopup();
  51.         }
  52.         else {
  53.             this.hidePopup();
  54.         }
  55.     }
  56.  
  57.     /* Testing Codes */
  58.     public static List<String> populateArray() {
  59.         List<String> test = new ArrayList<String>();
  60.         test.add("");
  61.         test.add("Mountain Flight");
  62.         test.add("Mount Climbing");
  63.         test.add("Trekking");
  64.         test.add("Rafting");
  65.         test.add("Jungle Safari");
  66.         test.add("Bungie Jumping");
  67.         test.add("Para Gliding");
  68.         return test;
  69.     }
  70.  
  71.     public static void makeUI() {
  72.         JFrame frame = new JFrame("Adventure in Nepal - Combo Filter Test");
  73.         FilterComboBoxStackOverflow acb = new FilterComboBoxStackOverflow(populateArray());
  74.         frame.getContentPane().add(acb);
  75.         frame.pack();
  76.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  77.         frame.setVisible(true);
  78.     }
  79.  
  80.     public static void main(String[] args) throws Exception {
  81.  
  82. //        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  83.         makeUI();
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement