Создайте текстовое поле автозаполнения в Java с выпадающим списком

Я хочу создать текстовое поле с автоматическим предложением, которое будет запрашивать базу данных при каждом событии выпуска ключа. Эта часть проста, но я хочу придать ей красивый визуальный ряд. Что-то похожее на текстовое поле автоматического предложения, которое мы видим на веб-сайтах, таких как поиск в Facebook.

Как сделать такой интерфейс?

Наивной идеей было бы разместить JList сразу под текстовым полем и сделать его видимым с результатами в нем при его обнаружении.

Любая лучшая идея или стандартный способ сделать это?


person jairaj    schedule 06.01.2013    source источник
comment
Посмотрите на этот существующий ответ   -  person atomman    schedule 07.01.2013
comment
это без выпадающего списка !! это можно сделать. Проблема заключается в том, как сделать раскрывающийся список с переменным размером в зависимости от количества результатов.   -  person jairaj    schedule 07.01.2013
comment
@jairaj все, что может быть достигнуто с помощью JTextField, можно легко перевести в JComboBox. Все, что вам нужно, это установить этот JTextField как ComboBoxEditor для JCombobox и сделать JComboBox редактируемым. Кстати, я бы посмотрел на SwingX, у которого уже есть довольно приличный компонент автозаполнения (посмотрите в Google)   -  person Guillaume Polet    schedule 07.01.2013


Ответы (7)


Ответ @ syb0rg проще, так как он использует стороннюю библиотеку.

Однако я использовал альтернативный подход:

Он использует пользовательский класс с именем AutoSuggestor, который принимает JTextField, его Window и ArrayList<String> слов для проверки введенных слов, цвет фона и цвет текста, а также цвет фокуса предложения, а также значение непрозрачности. При передаче ссылки JTextField добавляется ссылка DocumentListener, которая будет проверять, какое слово набрано, и отображать ли подсказки или нет, и если да, то какие подсказки отображать. Когда слово набирается, DocumentListener запускает методwordTyped(String wordTyped) с текущим набираемым словом или (по крайней мере, сколько слов когда-либо было набрано) в wordTyped(..) слово будет сравниваться со словами в AutoSuggestors классах словарь, который является базовым ArrayList из String, его можно установить на лету, как показано в приведенном ниже примере:

введите здесь описание изображения

(На данный момент вам нужно будет использовать мышь и щелкнуть слово, которое вы хотите автоматически заполнить, или использовать DOWN, чтобы пересечь предложения и текстовое поле и ENTER для выбора предложения при обходе с помощью клавиши вниз. Я еще не реализовал UP):

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 * @author David
 */
public class Test {

    public Test() {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JTextField f = new JTextField(10);

        AutoSuggestor autoSuggestor = new AutoSuggestor(f, frame, null, Color.WHITE.brighter(), Color.BLUE, Color.RED, 0.75f) {
            @Override
            boolean wordTyped(String typedWord) {

                //create list for dictionary this in your case might be done via calling a method which queries db and returns results as arraylist
                ArrayList<String> words = new ArrayList<>();
                words.add("hello");
                words.add("heritage");
                words.add("happiness");
                words.add("goodbye");
                words.add("cruel");
                words.add("car");
                words.add("war");
                words.add("will");
                words.add("world");
                words.add("wall");


                setDictionary(words);
                //addToDictionary("bye");//adds a single word

                return super.wordTyped(typedWord);//now call super to check for any matches against newest dictionary
            }
        };

        JPanel p = new JPanel();

        p.add(f);

        frame.add(p);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }
}

class AutoSuggestor {

    private final JTextField textField;
    private final Window container;
    private JPanel suggestionsPanel;
    private JWindow autoSuggestionPopUpWindow;
    private String typedWord;
    private final ArrayList<String> dictionary = new ArrayList<>();
    private int currentIndexOfSpace, tW, tH;
    private DocumentListener documentListener = new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent de) {
            checkForAndShowSuggestions();
        }

        @Override
        public void removeUpdate(DocumentEvent de) {
            checkForAndShowSuggestions();
        }

        @Override
        public void changedUpdate(DocumentEvent de) {
            checkForAndShowSuggestions();
        }
    };
    private final Color suggestionsTextColor;
    private final Color suggestionFocusedColor;

    public AutoSuggestor(JTextField textField, Window mainWindow, ArrayList<String> words, Color popUpBackground, Color textColor, Color suggestionFocusedColor, float opacity) {
        this.textField = textField;
        this.suggestionsTextColor = textColor;
        this.container = mainWindow;
        this.suggestionFocusedColor = suggestionFocusedColor;
        this.textField.getDocument().addDocumentListener(documentListener);

        setDictionary(words);

        typedWord = "";
        currentIndexOfSpace = 0;
        tW = 0;
        tH = 0;

        autoSuggestionPopUpWindow = new JWindow(mainWindow);
        autoSuggestionPopUpWindow.setOpacity(opacity);

        suggestionsPanel = new JPanel();
        suggestionsPanel.setLayout(new GridLayout(0, 1));
        suggestionsPanel.setBackground(popUpBackground);

        addKeyBindingToRequestFocusInPopUpWindow();
    }

    private void addKeyBindingToRequestFocusInPopUpWindow() {
        textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "Down released");
        textField.getActionMap().put("Down released", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {//focuses the first label on popwindow
                for (int i = 0; i < suggestionsPanel.getComponentCount(); i++) {
                    if (suggestionsPanel.getComponent(i) instanceof SuggestionLabel) {
                        ((SuggestionLabel) suggestionsPanel.getComponent(i)).setFocused(true);
                        autoSuggestionPopUpWindow.toFront();
                        autoSuggestionPopUpWindow.requestFocusInWindow();
                        suggestionsPanel.requestFocusInWindow();
                        suggestionsPanel.getComponent(i).requestFocusInWindow();
                        break;
                    }
                }
            }
        });
        suggestionsPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "Down released");
        suggestionsPanel.getActionMap().put("Down released", new AbstractAction() {
            int lastFocusableIndex = 0;

            @Override
            public void actionPerformed(ActionEvent ae) {//allows scrolling of labels in pop window (I know very hacky for now :))

                ArrayList<SuggestionLabel> sls = getAddedSuggestionLabels();
                int max = sls.size();

                if (max > 1) {//more than 1 suggestion
                    for (int i = 0; i < max; i++) {
                        SuggestionLabel sl = sls.get(i);
                        if (sl.isFocused()) {
                            if (lastFocusableIndex == max - 1) {
                                lastFocusableIndex = 0;
                                sl.setFocused(false);
                                autoSuggestionPopUpWindow.setVisible(false);
                                setFocusToTextField();
                                checkForAndShowSuggestions();//fire method as if document listener change occured and fired it

                            } else {
                                sl.setFocused(false);
                                lastFocusableIndex = i;
                            }
                        } else if (lastFocusableIndex <= i) {
                            if (i < max) {
                                sl.setFocused(true);
                                autoSuggestionPopUpWindow.toFront();
                                autoSuggestionPopUpWindow.requestFocusInWindow();
                                suggestionsPanel.requestFocusInWindow();
                                suggestionsPanel.getComponent(i).requestFocusInWindow();
                                lastFocusableIndex = i;
                                break;
                            }
                        }
                    }
                } else {//only a single suggestion was given
                    autoSuggestionPopUpWindow.setVisible(false);
                    setFocusToTextField();
                    checkForAndShowSuggestions();//fire method as if document listener change occured and fired it
                }
            }
        });
    }

    private void setFocusToTextField() {
        container.toFront();
        container.requestFocusInWindow();
        textField.requestFocusInWindow();
    }

    public ArrayList<SuggestionLabel> getAddedSuggestionLabels() {
        ArrayList<SuggestionLabel> sls = new ArrayList<>();
        for (int i = 0; i < suggestionsPanel.getComponentCount(); i++) {
            if (suggestionsPanel.getComponent(i) instanceof SuggestionLabel) {
                SuggestionLabel sl = (SuggestionLabel) suggestionsPanel.getComponent(i);
                sls.add(sl);
            }
        }
        return sls;
    }

    private void checkForAndShowSuggestions() {
        typedWord = getCurrentlyTypedWord();

        suggestionsPanel.removeAll();//remove previos words/jlabels that were added

        //used to calcualte size of JWindow as new Jlabels are added
        tW = 0;
        tH = 0;

        boolean added = wordTyped(typedWord);

        if (!added) {
            if (autoSuggestionPopUpWindow.isVisible()) {
                autoSuggestionPopUpWindow.setVisible(false);
            }
        } else {
            showPopUpWindow();
            setFocusToTextField();
        }
    }

    protected void addWordToSuggestions(String word) {
        SuggestionLabel suggestionLabel = new SuggestionLabel(word, suggestionFocusedColor, suggestionsTextColor, this);

        calculatePopUpWindowSize(suggestionLabel);

        suggestionsPanel.add(suggestionLabel);
    }

    public String getCurrentlyTypedWord() {//get newest word after last white spaceif any or the first word if no white spaces
        String text = textField.getText();
        String wordBeingTyped = "";
        if (text.contains(" ")) {
            int tmp = text.lastIndexOf(" ");
            if (tmp >= currentIndexOfSpace) {
                currentIndexOfSpace = tmp;
                wordBeingTyped = text.substring(text.lastIndexOf(" "));
            }
        } else {
            wordBeingTyped = text;
        }
        return wordBeingTyped.trim();
    }

    private void calculatePopUpWindowSize(JLabel label) {
        //so we can size the JWindow correctly
        if (tW < label.getPreferredSize().width) {
            tW = label.getPreferredSize().width;
        }
        tH += label.getPreferredSize().height;
    }

    private void showPopUpWindow() {
        autoSuggestionPopUpWindow.getContentPane().add(suggestionsPanel);
        autoSuggestionPopUpWindow.setMinimumSize(new Dimension(textField.getWidth(), 30));
        autoSuggestionPopUpWindow.setSize(tW, tH);
        autoSuggestionPopUpWindow.setVisible(true);

        int windowX = 0;
        int windowY = 0;

        windowX = container.getX() + textField.getX() + 5;
        if (suggestionsPanel.getHeight() > autoSuggestionPopUpWindow.getMinimumSize().height) {
            windowY = container.getY() + textField.getY() + textField.getHeight() + autoSuggestionPopUpWindow.getMinimumSize().height;
        } else {
            windowY = container.getY() + textField.getY() + textField.getHeight() + autoSuggestionPopUpWindow.getHeight();
        }

        autoSuggestionPopUpWindow.setLocation(windowX, windowY);
        autoSuggestionPopUpWindow.setMinimumSize(new Dimension(textField.getWidth(), 30));
        autoSuggestionPopUpWindow.revalidate();
        autoSuggestionPopUpWindow.repaint();

    }

    public void setDictionary(ArrayList<String> words) {
        dictionary.clear();
        if (words == null) {
            return;//so we can call constructor with null value for dictionary without exception thrown
        }
        for (String word : words) {
            dictionary.add(word);
        }
    }

    public JWindow getAutoSuggestionPopUpWindow() {
        return autoSuggestionPopUpWindow;
    }

    public Window getContainer() {
        return container;
    }

    public JTextField getTextField() {
        return textField;
    }

    public void addToDictionary(String word) {
        dictionary.add(word);
    }

    boolean wordTyped(String typedWord) {

        if (typedWord.isEmpty()) {
            return false;
        }
        //System.out.println("Typed word: " + typedWord);

        boolean suggestionAdded = false;

        for (String word : dictionary) {//get words in the dictionary which we added
            boolean fullymatches = true;
            for (int i = 0; i < typedWord.length(); i++) {//each string in the word
                if (!typedWord.toLowerCase().startsWith(String.valueOf(word.toLowerCase().charAt(i)), i)) {//check for match
                    fullymatches = false;
                    break;
                }
            }
            if (fullymatches) {
                addWordToSuggestions(word);
                suggestionAdded = true;
            }
        }
        return suggestionAdded;
    }
}

class SuggestionLabel extends JLabel {

    private boolean focused = false;
    private final JWindow autoSuggestionsPopUpWindow;
    private final JTextField textField;
    private final AutoSuggestor autoSuggestor;
    private Color suggestionsTextColor, suggestionBorderColor;

    public SuggestionLabel(String string, final Color borderColor, Color suggestionsTextColor, AutoSuggestor autoSuggestor) {
        super(string);

        this.suggestionsTextColor = suggestionsTextColor;
        this.autoSuggestor = autoSuggestor;
        this.textField = autoSuggestor.getTextField();
        this.suggestionBorderColor = borderColor;
        this.autoSuggestionsPopUpWindow = autoSuggestor.getAutoSuggestionPopUpWindow();

        initComponent();
    }

    private void initComponent() {
        setFocusable(true);
        setForeground(suggestionsTextColor);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                super.mouseClicked(me);

                replaceWithSuggestedText();

                autoSuggestionsPopUpWindow.setVisible(false);
            }
        });

        getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), "Enter released");
        getActionMap().put("Enter released", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                replaceWithSuggestedText();
                autoSuggestionsPopUpWindow.setVisible(false);
            }
        });
    }

    public void setFocused(boolean focused) {
        if (focused) {
            setBorder(new LineBorder(suggestionBorderColor));
        } else {
            setBorder(null);
        }
        repaint();
        this.focused = focused;
    }

    public boolean isFocused() {
        return focused;
    }

    private void replaceWithSuggestedText() {
        String suggestedWord = getText();
        String text = textField.getText();
        String typedWord = autoSuggestor.getCurrentlyTypedWord();
        String t = text.substring(0, text.lastIndexOf(typedWord));
        String tmp = t + text.substring(text.lastIndexOf(typedWord)).replace(typedWord, suggestedWord);
        textField.setText(tmp + " ");
    }
}

В настоящее время единственными возможными необходимыми дополнениями IMO являются:

  • Клавиша ВВЕРХ Сосредоточьте внимание на элементах во всплывающем окне автопредложений, чтобы мы могли двигаться вверх.

Если есть какие-то перегибы, дай мне знать, я посмотрю, что я могу сделать. Но, кажется, работает нормально (постучите по дереву).

person David Kroukamp    schedule 07.01.2013
comment
Неплохо! но когда я печатаю, фокус теряется из текстового поля. Мне нужно снова щелкнуть текстовое поле, чтобы снова начать печатать. Итак, для каждого символа, который я печатаю, мне приходилось принудительно фокусироваться на текстовом поле и типе. Вы столкнулись с этой проблемой? Если да, то исправили? - person arunram; 30.09.2013
comment
@arunram то же самое случилось со мной. Этот код отлично работал в Windows 7, но не в Machintosh OS X Yosemite (10.10.2). Я все еще пытаюсь это исправить, но я не эксперт, поэтому, возможно, это займет много времени. Не обещаю, но я буду стараться изо всех сил. - person panoet; 17.01.2016
comment
Мне больше всего нравится это решение. Другие были более элегантными, но этот легче взломать и извлечь из него уроки. - person Scott Grodberg; 28.02.2016
comment
Я столкнулся с несколькими проблемами при запуске этого кода. Окно текстовой области теряет фокус и прерывается при наборе текста. Также программа часто вылетает. Не могли бы вы исправить свой код или предложить некоторые вероятные причины? - person agassaa; 22.02.2017
comment
Если кто-то использует это и получает проблему с вводом текста в поле, добавьте условие для (int i = 0; i ‹ typedWord.length() && i ‹ word.length(); i++) в цикл for внутри wordTyped . Это предотвращает выход цикла за границы, если введенное слово больше предлагаемого слова. - person Parzavil; 16.06.2020
comment
Я пробовал, можно заменить JWindow на JPopMenu и все проблемы решатся. - person passerbywhu; 22.06.2020

Очень простой способ сделать это — использовать GlazedList реализацию автодополнения. Это очень легко встать и работать. Вы можете найти его здесь.

Вы можете установить автозаполнение на JComboBox только с одной строкой кода Glazed, например:

JComboBox comboBox = new JComboBox();
Object[] elements = new Object[] {"Cat", "Dog", "Lion", "Mouse"};
AutoCompleteSupport.install(comboBox, GlazedLists.eventListOf(elements));

Также SwingX поддерживает автозаполнение и может быть проще в использовании, чем GlazedList. Все, что вы пишете с SwingX, это AutoCompleteDecorator.decorate(comboBox);

person syb0rg    schedule 06.01.2013
comment
Все, что вы пишете с помощью SwingX, это AutoCompleteDecorator.decorate(comboBox); Отличный ответ!!! Почему я нашел это только через 4 дня? - person Blocked; 14.07.2013
comment
Я получаю эту ошибку для вашего кода: AutoCompleteSupport must be accessed from the Swing Event Dispatch Thread. - person Tomáš Zato - Reinstate Monica; 16.03.2015
comment
я не могу найти jar-файл GlazedLists.eventListOf, можете ли вы помочь мне найти jar-файл для этого. - person heshjse; 23.05.2015
comment
@syb0rg :+1. Самый простой ответ, который я нашел. Но как сделать то же самое из базы данных. Я был бы очень признателен, если бы вы предложили на это - person mustangDC; 13.06.2015
comment
сохранить результаты базы данных в массиве :) - person triForce420; 01.09.2015

Чтобы использовать класс TextAutoCompleter, вам необходимо загрузить jar-файл AutoCompleter.jar и добавить его в папку библиотеки вашего проекта. Вот ссылка для загрузки: http://download1689.mediafire.com/4grrthscpsug/7pwzgefiomu392o/AutoCompleter.jar - Nawin

//В классе Main пишем следующий код

package autocomplete;

import com.mxrck.autocompleter.TextAutoCompleter;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JTextField;


public class AutoComplete {
    JFrame f=new JFrame();
    JTextField t1;
AutoComplete() throws ClassNotFoundException, SQLException{

    f.setSize(500,500);
    f.setLocation(500,100);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(null);
    f.setVisible(true);


    t1=new JTextField();
    t1.setBounds(50,80,200,20);
    f.add(t1);


    TextAutoCompleter complete=new TextAutoCompleter(t1);
    DBConn conn=new DBConn();
        conn.connection();
        conn.retrieve();
    while(conn.rs.next()){

        complete.addItem(conn.rs.getString("number"));
    }


}


    public static void main(String[] args) throws ClassNotFoundException,
    SQLException{           

        new AutoComplete();
   }

}


//Create seperate class for database connection and write the following code


package autocomplete;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DBConn {

    Connection con; ResultSet rs;PreparedStatement stat;

public void connection() throws ClassNotFoundException, SQLException{
    String url="jdbc:mysql://localhost:3306/";
    String driver="com.mysql.jdbc.Driver";   
    String db="demo";
    String username="root";
    String password="root";
    stat =null; 

        Class.forName(driver);
       con=(Connection)DriverManager.getConnection
       (url+db,username,password);              
        System.out.println("Connecttion SuccessFul");
}  

public void retrieve() throws SQLException{

    Statement  stmt=con.createStatement();
    String query="select number from phone";
    rs = stmt.executeQuery(query);

    System.out.println("retrieve succesfully");

}

}

person nawin khatiwada    schedule 17.09.2015
comment
khawatiwada Спасибо за это предложение. Я искал в Интернете и наткнулся на это. Я хочу использовать его с HTTP-сервером, таким как WAMP, где я буду вызывать свою строку через API RESTFul. Только один вопрос из вашего ясного примера: как использовать строку, введенную в текстовое поле, в качестве параметра поиска? Это действительно не ясно для меня на вашем примере выше. Спасибо - person scruffycoder86; 16.10.2015

Создан на основе Дэвида решение:

Я добавил функциональность для клавиши UP, а также добавил функциональность для клавиши ESC, чтобы скрыть всплывающее окно. В дополнение к этому вы можете указать функцию обратного вызова при построении объекта AutoSuggestor, которая будет вызываться при выборе предложения из списка.

import javax.swing.border.LineBorder
import java.util.ArrayList
import javax.swing.event.DocumentListener

import java.awt.*
import java.awt.event.*
import javax.swing.*
import javax.swing.event.DocumentEvent

/**
 * Author of the original version: David @ https://stackoverflow.com/users/1133011/david-kroukamp
 */
class Test {
    init {

        val frame = JFrame()
        frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE


        val f = JTextField(10)

        val autoSuggestor = object : AutoSuggestor(f, frame, ArrayList(), Color.WHITE.brighter(), Color.BLUE, Color.RED, 0.75f) {
            override fun wordTyped(typedWord: String?): Boolean {

                //create list for dictionary this in your case might be done via calling a method which queries db and returns results as arraylist
                val words = ArrayList<String>()
                words.add("hello")
                words.add("heritage")
                words.add("happiness")
                words.add("goodbye")
                words.add("cruel")
                words.add("car")
                words.add("war")
                words.add("will")
                words.add("world")
                words.add("wall")


                setDictionary(words)
                //addToDictionary("bye");//adds a single word

                return super.wordTyped(typedWord)//now call super to check for any matches against newest dictionary
            }
        }

        val p = JPanel()

        p.add(f)

        frame.add(p)

        frame.pack()
        frame.isVisible = true
    }

    companion object {

        @JvmStatic
        fun main(args: Array<String>) {
            SwingUtilities.invokeLater { Test() }
        }
    }
}

internal open class AutoSuggestor(val textField: JTextField, val container: Window, words: ArrayList<String>, popUpBackground: Color, private val suggestionsTextColor: Color, private val suggestionFocusedColor: Color, opacity: Float, private val callback: (String) -> Unit = {}) {
    private val suggestionsPanel: JPanel
    val autoSuggestionPopUpWindow: JWindow
    private var typedWord: String? = null
    private val dictionary = ArrayList<String>()
    private var currentIndexOfSpace: Int = 0
    private var tW: Int = 0
    private var tH: Int = 0
    private val documentListener = object : DocumentListener {
        override fun insertUpdate(de: DocumentEvent) {
            checkForAndShowSuggestions()
        }

        override fun removeUpdate(de: DocumentEvent) {
            checkForAndShowSuggestions()
        }

        override fun changedUpdate(de: DocumentEvent) {
            checkForAndShowSuggestions()
        }
    }

    val addedSuggestionLabels: ArrayList<SuggestionLabel>
        get() {
            val sls = ArrayList<SuggestionLabel>()
            for (i in 0 until suggestionsPanel.componentCount) {
                if (suggestionsPanel.getComponent(i) is SuggestionLabel) {
                    val sl = suggestionsPanel.getComponent(i) as SuggestionLabel
                    sls.add(sl)
                }
            }
            return sls
        }

    //get newest word after last white space if any or the first word if no white spaces
    val currentlyTypedWord: String
        get() {
            val text = textField.text
            var wordBeingTyped = ""
            if (text.contains(" ")) {
                val tmp = text.lastIndexOf(" ")
                if (tmp >= currentIndexOfSpace) {
                    currentIndexOfSpace = tmp
                    wordBeingTyped = text.substring(text.lastIndexOf(" "))
                }
            } else {
                wordBeingTyped = text
            }
            return wordBeingTyped.trim { it <= ' ' }
        }

    init {
        this.textField.document.addDocumentListener(documentListener)

        setDictionary(words)

        typedWord = ""
        currentIndexOfSpace = 0
        tW = 0
        tH = 0

        autoSuggestionPopUpWindow = JWindow(container)
        autoSuggestionPopUpWindow.opacity = opacity

        suggestionsPanel = JPanel()
        suggestionsPanel.layout = GridLayout(0, 1)
        suggestionsPanel.background = popUpBackground

        addFocusListenersToHandleVisibilityOfPopUpWindow()
        addKeyBindingToRequestFocusInPopUpWindow()
    }

    private fun addFocusListenersToHandleVisibilityOfPopUpWindow() {
        textField.addFocusListener(object:FocusListener {
            override fun focusLost(e: FocusEvent?) {
                var focusOnPopUp = false
                for (i in 0 until suggestionsPanel.componentCount) {
                    if (suggestionsPanel.getComponent(i) is SuggestionLabel) {
                        val label = suggestionsPanel.getComponent(i) as SuggestionLabel
                        if (label.isFocused)
                            focusOnPopUp = true
                    }
                }

                if (!focusOnPopUp && !shouldShowPopUpWindow) {
                    autoSuggestionPopUpWindow.isVisible = false
                }
            }
            override fun focusGained(e: FocusEvent?) {
                shouldShowPopUpWindow = false
            }
        })
    }

    private fun addKeyBindingToRequestFocusInPopUpWindow() {
        textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true), "Escape released")
        textField.actionMap.put("Escape released", object : AbstractAction() {
            override fun actionPerformed(ae: ActionEvent) {// Hide the popwindow
                shouldShowPopUpWindow = false
                autoSuggestionPopUpWindow.isVisible = false
            }
        })


        textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "Down released")
        textField.actionMap.put("Down released", object : AbstractAction() {
            override fun actionPerformed(ae: ActionEvent) {//focuses the first label on popwindow
                for (i in 0 until suggestionsPanel.componentCount) {
                    if (suggestionsPanel.getComponent(i) is SuggestionLabel) {
                        (suggestionsPanel.getComponent(i) as SuggestionLabel).isFocused = true
                        autoSuggestionPopUpWindow.toFront()
                        autoSuggestionPopUpWindow.requestFocusInWindow()
                        suggestionsPanel.requestFocusInWindow()
                        suggestionsPanel.getComponent(i).requestFocusInWindow()
                        break
                    }
                }
            }
        })

        textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "Up released")
        textField.actionMap.put("Up released", object : AbstractAction() {
            override fun actionPerformed(ae: ActionEvent) {//focuses the last label on popwindow
                for (i in 0 until suggestionsPanel.componentCount) {
                    val reverseIndex = suggestionsPanel.componentCount-1 - i
                    if (suggestionsPanel.getComponent(reverseIndex) is SuggestionLabel) {
                        (suggestionsPanel.getComponent(reverseIndex) as SuggestionLabel).isFocused = true
                        autoSuggestionPopUpWindow.toFront()
                        autoSuggestionPopUpWindow.requestFocusInWindow()
                        suggestionsPanel.requestFocusInWindow()
                        suggestionsPanel.getComponent(reverseIndex).requestFocusInWindow()
                        break
                    }
                }
            }
        })

        suggestionsPanel.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true), "Escape released")
        suggestionsPanel.actionMap.put("Escape released", object : AbstractAction() {
            override fun actionPerformed(ae: ActionEvent) {// Hide the popwindow
                shouldShowPopUpWindow = false
                autoSuggestionPopUpWindow.isVisible = false
            }
        })
        suggestionsPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "Up released")
        suggestionsPanel.actionMap.put("Up released", object : AbstractAction() {
            override fun actionPerformed(ae: ActionEvent) {//allows scrolling of labels in pop window (I know very hacky for now :))

                val sls = addedSuggestionLabels
                val max = sls.size

                var indexOfFocusedSuggestion = -1
                for (i in 0 until max) {
                    val sl = sls[i]
                    if ( sl.isFocused )
                        indexOfFocusedSuggestion = i
                }

                if (indexOfFocusedSuggestion - 1 < 0) {
                    sls[indexOfFocusedSuggestion].isFocused = false
                    autoSuggestionPopUpWindow.isVisible = false
                    setFocusToTextField()
                    checkForAndShowSuggestions()//fire method as if document listener change occured and fired it
                }
                else {
                    sls[indexOfFocusedSuggestion].isFocused = false
                    sls[indexOfFocusedSuggestion-1].isFocused = true
                    autoSuggestionPopUpWindow.toFront()
                    autoSuggestionPopUpWindow.requestFocusInWindow()
                    suggestionsPanel.requestFocusInWindow()
                    suggestionsPanel.getComponent(indexOfFocusedSuggestion-1).requestFocusInWindow()
                }

            }
        })
        suggestionsPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "Down released")
        suggestionsPanel.actionMap.put("Down released", object : AbstractAction() {
            override fun actionPerformed(ae: ActionEvent) {//allows scrolling of labels in pop window (I know very hacky for now :))

                val sls = addedSuggestionLabels
                val max = sls.size

                var indexOfFocusedSuggestion = -1
                for (i in 0 until max) {
                    val sl = sls[i]
                    if ( sl.isFocused )
                        indexOfFocusedSuggestion = i
                }

                if (indexOfFocusedSuggestion + 1 >= max) {
                    sls[indexOfFocusedSuggestion].isFocused = false
                    autoSuggestionPopUpWindow.isVisible = false
                    setFocusToTextField()
                    checkForAndShowSuggestions()//fire method as if document listener change occured and fired it
                }
                else {
                    sls[indexOfFocusedSuggestion].isFocused = false
                    sls[indexOfFocusedSuggestion+1].isFocused = true
                    autoSuggestionPopUpWindow.toFront()
                    autoSuggestionPopUpWindow.requestFocusInWindow()
                    suggestionsPanel.requestFocusInWindow()
                    suggestionsPanel.getComponent(indexOfFocusedSuggestion+1).requestFocusInWindow()
                }
            }

        })
    }

    private fun setFocusToTextField() {
        container.toFront()
        container.requestFocusInWindow()
        textField.requestFocusInWindow()
    }

    var shouldShowPopUpWindow = false

    private fun checkForAndShowSuggestions() {
        typedWord = currentlyTypedWord

        suggestionsPanel.removeAll()//remove previos words/jlabels that were added

        //used to calcualte size of JWindow as new Jlabels are added
        tW = 0
        tH = 0

        val added = wordTyped(typedWord)

        if (!added) {
            if (autoSuggestionPopUpWindow.isVisible) {
                autoSuggestionPopUpWindow.isVisible = false
            }
        } else {
            shouldShowPopUpWindow = true
            showPopUpWindow()
            setFocusToTextField()
        }
    }

    protected fun addWordToSuggestions(word: String) {
        val suggestionLabel = SuggestionLabel(word, suggestionFocusedColor, suggestionsTextColor, this, callback)

        calculatePopUpWindowSize(suggestionLabel)

        suggestionsPanel.add(suggestionLabel)
    }

    private fun calculatePopUpWindowSize(label: JLabel) {
        //so we can size the JWindow correctly
        if (tW < label.preferredSize.width) {
            tW = label.preferredSize.width
        }
        tH += label.preferredSize.height
    }

    private fun showPopUpWindow() {
        autoSuggestionPopUpWindow.contentPane.add(suggestionsPanel)
        autoSuggestionPopUpWindow.minimumSize = Dimension(textField.width, 30)
        autoSuggestionPopUpWindow.setSize(tW, tH)
        autoSuggestionPopUpWindow.isVisible = true

        var windowX = 0
        var windowY = 0

        windowX = container.getX() + textField.x + 5
        if (suggestionsPanel.height > autoSuggestionPopUpWindow.minimumSize.height) {
            windowY = container.getY() + textField.y + textField.height + autoSuggestionPopUpWindow.minimumSize.height
        } else {
            windowY = container.getY() + textField.y + textField.height + autoSuggestionPopUpWindow.height
        }

        autoSuggestionPopUpWindow.setLocation(windowX, windowY)
        autoSuggestionPopUpWindow.minimumSize = Dimension(textField.width, 30)
        autoSuggestionPopUpWindow.revalidate()
        autoSuggestionPopUpWindow.repaint()

    }

    fun setDictionary(words: ArrayList<String>?) {
        dictionary.clear()
        if (words == null) {
            return //so we can call constructor with null value for dictionary without exception thrown
        }
        for (word in words) {
            dictionary.add(word)
        }
    }

    fun addToDictionary(word: String) {
        dictionary.add(word)
    }

    open fun wordTyped(typedWord: String?): Boolean {

        if (typedWord!!.isEmpty()) {
            return false
        }

        var suggestionAdded = false

        for (word in dictionary) {//get words in the dictionary which we added
            var fullyMatches = word.length >= typedWord.length
            for (i in 0 until typedWord.length) {//each string in the word
                if (word.length > i && !typedWord.toLowerCase().startsWith(word.toLowerCase()[i].toString(), i)) {//check for match
                    fullyMatches = false
                    break
                }
            }
            if (fullyMatches) {
                addWordToSuggestions(word)
                suggestionAdded = true
            }
        }
        return suggestionAdded
    }
}

internal class SuggestionLabel(string: String, private val suggestionBorderColor: Color, private val suggestionsTextColor: Color, private val autoSuggestor: AutoSuggestor, private val callback: (String) -> Unit) : JLabel(string) {

    var isFocused = false
        set(focused) {
            if (focused) {
                border = LineBorder(suggestionBorderColor)
            } else {
                border = null
            }
            repaint()
            field = focused
        }
    private val autoSuggestionsPopUpWindow: JWindow
    private val textField: JTextField

    init {
        this.textField = autoSuggestor.textField
        this.autoSuggestionsPopUpWindow = autoSuggestor.autoSuggestionPopUpWindow

        initComponent()
    }

    private fun initComponent() {
        isFocusable = true
        foreground = suggestionsTextColor

        addMouseListener(object : MouseAdapter() {
            override fun mouseClicked(me: MouseEvent) {
                super.mouseClicked(me)

                replaceWithSuggestedText()

                autoSuggestionsPopUpWindow.isVisible = false
            }
        })

        getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), "Enter released")
        actionMap.put("Enter released", object : AbstractAction() {
            override fun actionPerformed(ae: ActionEvent) {
                replaceWithSuggestedText()
                autoSuggestionsPopUpWindow.isVisible = false
            }
        })

        getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true), "Escape released")
        actionMap.put("Escape released", object : AbstractAction() {
            override fun actionPerformed(ae: ActionEvent) {// Hide the popwindow
                autoSuggestionsPopUpWindow.isVisible = false
            }
        })
    }

    private fun replaceWithSuggestedText() {

        val suggestedWord = text
        val text = textField.text
        val typedWord = autoSuggestor.currentlyTypedWord
        val t = text.substring(0, text.lastIndexOf(typedWord))
        val tmp = t + text.substring(text.lastIndexOf(typedWord)).replace(typedWord, suggestedWord)
        textField.text = tmp
        callback(tmp)
    }
}

Примечание. Вышеприведенное написано на Kotlin, но если вам действительно нужен код Java, вы можете легко преобразовать его обратно в Java.

person Alexios    schedule 25.02.2019

Мне нужно было автозаполнение для редактора в моей IDE ассемблера AVR, поэтому я написал реализацию, которая работает так же, как автозаполнение в Eclipse (активация CTRL-SPACE, выпадающий список с полосами прокрутки, клавиши курсора + навигация мышью). Он не имеет внешних зависимостей и представляет собой всего лишь один класс. Это должно работать для всех подклассов JTextComponent; вы можете найти пример использования в папке src/test.

person Community    schedule 28.01.2017

добавьте эти строки в private void addKeyBindingToRequestFocusInPopUpWindow() первого ответа, чтобы реализовать ключ UP. Он отвечает идеально.

//here I have to do my code for up key
    //---------------------------------------------------------------------
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "Up released");
    textField.getActionMap().put("Up released", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ae) {//focuses the first label on popwindow
            for (int i = suggestionsPanel.getComponentCount()-1; i >=0; i--) {
                if (suggestionsPanel.getComponent(i) instanceof SuggestionLabel) {
                    ((SuggestionLabel) suggestionsPanel.getComponent(i)).setFocused(true);
                    autoSuggestionPopUpWindow.toFront();
                    autoSuggestionPopUpWindow.requestFocusInWindow();
                    suggestionsPanel.requestFocusInWindow();
                    suggestionsPanel.getComponent(i).requestFocusInWindow();
                    break;
                }
            }
        }
    });

    suggestionsPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "Up released");
    suggestionsPanel.getActionMap().put("Up released", new AbstractAction() {
        //######int lastFocusableIndex = 0;
        int lastFocusableIndex = 0;
        //lastFocusableIndex=lastFocusableIndex___;
        @Override
        public void actionPerformed(ActionEvent ae) {//allows scrolling of labels in pop window (I know very hacky for now :))

            ArrayList<SuggestionLabel> sls = getAddedSuggestionLabels();
            int max = sls.size();
            lastFocusableIndex=lastFocusableIndex___;

            System.out.println("UP UP UP UP");//***// 
            System.out.println("max = "+String.valueOf(max));//***// 
            System.out.println("lastFocusableIndex = "+String.valueOf(lastFocusableIndex));//***// 
            System.out.println("UP UP UP UP");//***// 

            if (max > 1) {//more than 1 suggestion
                for (int i = max-1; i >=0; i--) {
                    SuggestionLabel sl = sls.get(i);
                    if (sl.isFocused()) {
                        if (lastFocusableIndex == 0) {
                            lastFocusableIndex = max - 1;
                            lastFocusableIndex___=lastFocusableIndex;
                            sl.setFocused(false);
                            autoSuggestionPopUpWindow.setVisible(false);
                            setFocusToTextField();
                            checkForAndShowSuggestions();//fire method as if document listener change occured and fired it

                        } else {
                            sl.setFocused(false);
                            lastFocusableIndex = i;
                            lastFocusableIndex___=lastFocusableIndex;
                        }
                    } else if (lastFocusableIndex > i) {
                        if (i < max ) {
                            sl.setFocused(true);
                            autoSuggestionPopUpWindow.toFront();
                            autoSuggestionPopUpWindow.requestFocusInWindow();
                            suggestionsPanel.requestFocusInWindow();
                            suggestionsPanel.getComponent(i).requestFocusInWindow();
                            lastFocusableIndex = i;
                            lastFocusableIndex___=lastFocusableIndex;
                            break;
                        }
                    }
                }
            } else {//only a single suggestion was given
                autoSuggestionPopUpWindow.setVisible(false);
                setFocusToTextField();
                checkForAndShowSuggestions();//fire method as if document listener change occured and fired it
            }
        }
    });
person Mohammad Reza Rezwani    schedule 10.02.2017

Рабочий подход, который я использовал в проекте, заключался в том, чтобы поместить JTextField поверх JComboBox и сделать базовое поле со списком открытым при вводе в JTextField с помощью прослушивателя документов. Вы, вероятно, хотите, чтобы пользовательская модель со списком позволяла более эффективно изменять элементы, поскольку модель по умолчанию, я думаю, позволяет добавлять элементы только по одному, что может быть ударом по производительности. чтобы открыть поле со списком, я думаю, есть способ показать его, если вы получите его пользовательский интерфейс. Я столкнулся с парой ошибок с прокруткой, когда пытался изменить элементы, когда он был открыт, поэтому вам может потребоваться закрыть его, изменить элементы и заново показать. Для клавиатуры вы можете поймать нажатия клавиш клавиатуры в JTextField и соответствующим образом вызвать JComboBox.

person William Dutton    schedule 19.08.2017