swing autocomplete text field / drop down
We need an autocomplete component for swing, the problem with the one in jdesktop/SwingX is that we have to use a combo box and after each key stroke it simply scrolls down to the nearest match but the combo still holds the 25.000 elements. It doesn't show the 4 or 5 that are the closest match together because they can be in different places of the list. We don't want to display a list with the 25000 eith开发者_如何学运维er...
Is there anything similar to the JSF autocomplete or the one in google main page? What we need is a component which asks our interface something like
public List getOptions(String typedSoFar) { //here we return the 5 matching ones according to our criteria and simply offers the //user those five }
A really easy way to do this is to use the GlazedList implementation of auto-completion. It's very easy to get up and running. You can find it here:
http://publicobject.com/glazedlists/
You can install the auto-complete on a JComboBox with only one line of Glazed code, like this:
JComboBox comboBox = new JComboBox(); Object[] elements = new Object[] {"Cat", "Dog", "Lion", "Mouse"}; AutoCompleteSupport.install(comboBox, GlazedLists.eventListOf(elements));
GlazedList Not very good, also SwingX support Auto-Complete and easier than "GlazedList". you only writing:
AutoCompleteDecorator.decorate(yourComboBox);
swingX is the best answer.
AutoCompleteDecorator.decorate(textComplete, strings, true);
where textComplete
is the textbox, strings
is the jList with the suggestion dictionary and last value is whether to restrict the contents of the text field to the dictionary or not.
I have not used SwingX and know nothing about their implementation. The best idea would probably be to extend the SwingX Autocomplete class. Instead of using the Autocomplete class directly, create a proprietary class extendig the Autocomplete class and override the getOptions() method:
class OurOwnAutocomplete extending swingx.*.*.autocomplete {
private List getOptions(String typedSoFar) { //
// logic
}
精彩评论