Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.testss;
- 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 FilterComboBoxStackOverflow extends JComboBox {
- private List<String> array;
- public FilterComboBoxStackOverflow(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) {
- DefaultComboBoxModel model = (DefaultComboBoxModel) this.getModel();
- model.removeAllElements();
- for (String s: filterArray)
- model.addElement(s);
- JTextField textfield = (JTextField) this.getEditor().getEditorComponent();
- textfield.setText(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");
- FilterComboBoxStackOverflow acb = new FilterComboBoxStackOverflow(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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement