4

I have this JComboBox that has its editable property true. I was wondering if I have a List<String> added to that JComboBox filtering be possible?

For example I have new String[] {"a", "a1", "b", "c", "a3"} added to a JComboBox having editable true property. When I entered a on the JComboBox would return a list of new String[] {"a", "a1", "a3"}.

Is this possible?

Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
David B
  • 3,269
  • 12
  • 48
  • 80

2 Answers2

4

Easiest of ways is use AutocComplete JComboBox / JTextField, then JComboBox'es popup list returns filtered Items

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • sure there are lots of variations about this option, but I'd suggest to don't use that, this code has correctly implemented all listeners, its events and notifiers too, – mKorbel May 14 '12 at 07:21
  • I agree, the question is where could I see that short-coded version of that? – David B May 14 '12 at 07:23
  • I forgot there is very good alternative SwingX have got implemented decorator for bunch of JComponents – mKorbel May 14 '12 at 07:24
  • I understood, about question `short-coded version of that` I think that not possible pack bunch of important methods to the "Zipped Version" – mKorbel May 14 '12 at 07:27
0

Here is what you need to do. create an arrayList or List and add all your items there. Create combobox and and loops all your items and add it to comboBox Then create a filter method. This is not a perfect answer but it will get you going.

public class FilterComboBoxText {

    private JFrame frame;
    private final JComboBox comboBox = new JComboBox();
    private static List<String>listForComboBox= new ArrayList<String>();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FilterComboBoxText window = new FilterComboBoxText();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        listForComboBox.add("Lion");
        listForComboBox.add("LionKing");
        listForComboBox.add("Mufasa");
        listForComboBox.add("Nala");
        listForComboBox.add("KingNala");
        listForComboBox.add("Animals");
        listForComboBox.add("Anims");
        listForComboBox.add("Fish");
        listForComboBox.add("Jelly Fish");
        listForComboBox.add("I am the boss");


    }

    public FilterComboBoxText() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 412, 165);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        comboBox.setEditable(true);

        comboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent arg0) {

            }
        });

        for(String detail : listForComboBox) {
            comboBox.addItem(detail); 
        }
        final JTextField textfield = (JTextField) comboBox.getEditor().getEditorComponent();
        textfield.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent ke) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        if(!textfield.getText().isEmpty()){
                            comboBoxFilter(textfield.getText());
                        }
                    }
                });

            }
        });
        comboBox.setBounds(10, 39, 364, 29);
        frame.getContentPane().add(comboBox);
    }
     public void comboBoxFilter(String enteredText) {
         System.out.println(comboBox.getItemCount());

            if (!comboBox.isPopupVisible()) {
                comboBox.showPopup();
            }

            List<String> filterArray= new ArrayList<String>();
            for (int i = 0; i < listForComboBox.size(); i++) {
                if (listForComboBox.get(i).toLowerCase().contains(enteredText.toLowerCase())) {
                    filterArray.add(listForComboBox.get(i));
                }
            }
            if (filterArray.size() > 0) {
                DefaultComboBoxModel<String> model = (DefaultComboBoxModel<String>) comboBox.getModel();
                model.removeAllElements();
                model.addElement("");
                for (String s: filterArray)
                    model.addElement(s);

                JTextField textfield = (JTextField) comboBox.getEditor().getEditorComponent();
                textfield.setText(enteredText);
            }
        }

}
kupaff
  • 329
  • 4
  • 5