Java JTextField Auto Complete setupAutoComplete(final JTextField textField, final List items, final String stringMethodType, final boolean ignoreCase)

Here you can find the source of setupAutoComplete(final JTextField textField, final List items, final String stringMethodType, final boolean ignoreCase)

Description

setup Auto Complete

License

LGPL

Declaration

public static void setupAutoComplete(final JTextField textField, final List<String> items,
        final String stringMethodType, final boolean ignoreCase) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import java.util.List;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;

import javax.swing.JComboBox;

import javax.swing.JTextField;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Main {

    public static void setupAutoComplete(final JTextField textField, final Map<String, String> itemMap,
            final String stringMethodType, final boolean ignoreCase) {
        final DefaultComboBoxModel model = new DefaultComboBoxModel(itemMap.keySet().toArray());
        final JComboBox combo = new JComboBox(model) {
            private static final long serialVersionUID = 1L;

            public Dimension getPreferredSize() {
                return new Dimension(super.getPreferredSize().width, 0);
            }/*  w ww  .  j  a  va  2  s. c  o m*/
        };
        combo.putClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting", false);
        combo.setSelectedItem(null);

        combo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!(Boolean) combo.getClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting")) {
                    Object item = combo.getSelectedItem();
                    if (item != null) {
                        textField.setText(itemMap.get(item));
                    }
                }
            }
        });

        textField.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                combo.putClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting", true);
                if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_UP
                        || e.getKeyCode() == KeyEvent.VK_DOWN) {
                    e.setSource(combo);
                    combo.dispatchEvent(e);
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        Object item = combo.getSelectedItem();
                        if (item != null) {
                            textField.setText(itemMap.get(item));
                        }
                        combo.setPopupVisible(false);
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    combo.setPopupVisible(false);
                }
                combo.putClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting", false);
            }
        });
        textField.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                updateList();
            }

            public void removeUpdate(DocumentEvent e) {
                updateList();
            }

            public void changedUpdate(DocumentEvent e) {
                updateList();
            }

            private void updateList() {
                combo.putClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting", true);
                model.removeAllElements();
                String text = textField.getText();
                if (ignoreCase)
                    text = text.toUpperCase();
                if (!text.isEmpty()) {
                    for (String item : itemMap.keySet()) {
                        String itemU = ignoreCase ? item.toUpperCase() : item;
                        if (stringMethodType.equals("startsWith")) {
                            if (itemU.startsWith(text)) {
                                model.addElement(item);
                            }
                        } else if (stringMethodType.equals("endsWith")) {
                            if (itemU.endsWith(text)) {
                                model.addElement(item);
                            }
                        } else if (stringMethodType.equals("contains")) {
                            if (itemU.contains(text)) {
                                model.addElement(item);
                            }
                        } else
                            throw new IllegalArgumentException(
                                    "The size of viewItems unequal to the size of modelItems!");
                    }
                }
                combo.setPopupVisible(model.getSize() > 0);
                combo.putClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting", false);
            }
        });
        textField.setLayout(new BorderLayout());
        textField.add(combo, BorderLayout.SOUTH);
    }

    public static void setupAutoComplete(final JTextField textField, final List<String> items,
            final String stringMethodType, final boolean ignoreCase) {
        final DefaultComboBoxModel model = new DefaultComboBoxModel(items.toArray());
        final JComboBox combo = new JComboBox(model) {
            private static final long serialVersionUID = 1L;

            public Dimension getPreferredSize() {
                return new Dimension(super.getPreferredSize().width, 0);
            }
        };
        combo.putClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting", false);
        combo.setSelectedItem(null);

        combo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!(Boolean) combo.getClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting")) {
                    Object item = combo.getSelectedItem();
                    if (item != null) {
                        textField.setText(item.toString());
                    }
                }
            }
        });

        textField.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                combo.putClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting", true);
                if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_UP
                        || e.getKeyCode() == KeyEvent.VK_DOWN) {
                    e.setSource(combo);
                    combo.dispatchEvent(e);
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        Object item = combo.getSelectedItem();
                        if (item != null) {
                            textField.setText(item.toString());
                        }
                        combo.setPopupVisible(false);
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    combo.setPopupVisible(false);
                }
                combo.putClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting", false);
            }
        });
        textField.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                updateList();
            }

            public void removeUpdate(DocumentEvent e) {
                updateList();
            }

            public void changedUpdate(DocumentEvent e) {
                updateList();
            }

            private void updateList() {
                combo.putClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting", true);
                model.removeAllElements();
                String text = textField.getText();
                if (ignoreCase)
                    text = text.toUpperCase();
                if (!text.isEmpty()) {
                    for (String item : items) {
                        String itemU = ignoreCase ? item.toUpperCase() : item;
                        if (stringMethodType.equals("startsWith")) {
                            if (itemU.startsWith(text)) {
                                model.addElement(item);
                            }
                        } else if (stringMethodType.equals("endsWith")) {
                            if (itemU.endsWith(text)) {
                                model.addElement(item);
                            }
                        } else if (stringMethodType.equals("contains")) {
                            if (itemU.contains(text)) {
                                model.addElement(item);
                            }
                        } else
                            throw new IllegalArgumentException(
                                    "The size of viewItems unequal to the size of modelItems!");
                    }
                }
                combo.setPopupVisible(model.getSize() > 0);
                combo.putClientProperty(Integer.toHexString(combo.hashCode()) + "@is_adjusting", false);
            }
        });
        textField.setLayout(new BorderLayout());
        textField.add(combo, BorderLayout.SOUTH);
    }
}

Related

  1. installAutoComplete(final JTextField textField, final List items, final String stringMethodType, final boolean ignoreCase)