JTextField tf = new JTextField(10); JAutoComplete autoCompleter = new JAutoComplete(tf);
autoCompleter.addToDictionary("hapiness"); autoCompleter.addToDictionary("hello"); autoCompleter.addToDictionary("heritage"); autoCompleter.addToDictionary("cruel world"); autoCompleter.addToDictionary("war"); autoCompleter.addToDictionary("wording"); autoCompleter.addToDictionary("world");For example, with this dictionary, the following Popup will be presented after typing the "h" key:
setOpacity(float)
: set the opacity of the Popup WindowsetCaseSensitive(boolean)
: true if case is sensitive for the searchsearchHitsFromStart(boolean)
: true if the Search for hits is performed from the start of the typed text only. For example the dictionary word "conf" would be a hit for the text "unconfirmed" if this property was set to false, and false if it was set to true. There would be a hit in the two cases for the text "confirmed"showPopup(boolean)
: true if a Popup Window presenting the possible hits should be presented. if false, the first hit will be automatically used for the completionacceptDuplicates(boolean)
: true if the dictionary accept duplicatesstartSearchOnKeyCode(int)
: the keyCode to use to start the search. By default the search is started immediately during the typing. If the keyCode is strictly greater than 0, the search will be started by typing this keyCode with the Control key downsearchPerWord(boolean)
: true if the Search is performed per each wordsetResultsLimit(int)
: allows to limit the number of results per category (-1 means no limitation)autoCompleter.addCategory("category 1"); autoCompleter.addCategory("category 2"); autoCompleter.addToDictionary("category 1", "hello"); autoCompleter.addToDictionary("category 1", "heritage"); autoCompleter.addToDictionary("category 1", "happiness"); autoCompleter.addToDictionary("category 1", "woodbye"); autoCompleter.addToDictionary("category 2", "cruel world"); autoCompleter.addToDictionary("category 2", "war"); autoCompleter.addToDictionary("category 2", "will"); autoCompleter.addToDictionary("category 2", "world"); autoCompleter.addToDictionary("category 2", "wall");The following Popup will be presented after typing the "w" key:
public static class MyAutoComplete extends AutoComplete { public MyAutoComplete(JTextComponent comp) { super(comp); } @Override public void addCategoryToSuggestions(Category category) { JLabel catLabel = new JLabel("<html><i><u>" + category.getName() + "</u></i></html>"); catLabel.setFont(textComponent.getFont()); Dimension prefSize = catLabel.getPreferredSize(); updatePopupSize(prefSize.width, prefSize.height); suggestedHitsPanel.add(catLabel); } }In that case, the suggestions will be separated for each category. If we have the following dictionary:
autoCompleter.addCategory("category 1"); autoCompleter.addCategory("category 2"); autoCompleter.addToDictionary("category 1", "hello"); autoCompleter.addToDictionary("category 1", "heritage"); autoCompleter.addToDictionary("category 1", "happiness"); autoCompleter.addToDictionary("category 1", "woodbye"); autoCompleter.addToDictionary("category 2", "cruel world"); autoCompleter.addToDictionary("category 2", "war"); autoCompleter.addToDictionary("category 2", "will"); autoCompleter.addToDictionary("category 2", "world"); autoCompleter.addToDictionary("category 2", "wall");The following Popup will be presented after typing the "w" key:
JTextField tf = new JTextField(10); JAutoComplete autoCompleter = new JAutoComplete(tf); autoCompleter.setOpacity(0.9f); autoCompleter.searchHitsFromStart(false); //add the dictionary autoCompleter.addToDictionary("hello"); autoCompleter.addToDictionary("highlight"); autoCompleter.addToDictionary("cruel world"); autoCompleter.addToDictionary("war"); autoCompleter.addToDictionary("wording"); autoCompleter.addToDictionary("world");
Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences