I have seen many questions on autocompeletion for java text components like
There are not many options either:
1) 3rd party library (like SwingX)
2) DIY (i.e using
JLabel etc and a few requestFocusInWindow calls)
I chose number 2 and put the code up here for others to have a working foundation that can be improved to your needs.
Basically you would just make sure the
AutoSuggestor class is within package hierarchy than you would do something like:
//create JTextComponent that we want to make as AutoSuggestor //JTextField f = new JTextField(10); JTextArea f = new JTextArea(10, 10); //JEditorPane f = new JEditorPane(); //create words for dictionary could also use null as parameter for AutoSuggestor(..,..,null,..,..,..,..) and than call AutoSuggestor#setDictionary after AutoSuggestr insatnce has been created 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"); AutoSuggestor autoSuggestor = new AutoSuggestor(f, frame, words, Color.WHITE.brighter(), Color.BLUE, Color.RED, 0.75f);
A suggestion cna be clicked with the mouse or alternatively the down key can be use to traverse suggestions and the textcomponent
JTextField pop up window will be shown under the component:
JTextArea and any other
JTextComponents will have it visible under the Carets current position:
Hope it helps others.
Edited by DavidKroukamp