Skip to main content

AutoComplete Enhancements

2 replies [Last post]
bierhance
Offline
Joined: 2003-06-11

Hello!

Today I finally found some time to work on our autocomplete code.

I incorporated a patch that was originally submitted by Noel Grandin to decouple the autocomplete feature from the items' toString method (sorry Noel for not being more responsive...).

It's now possible to supply a converter object that determines the string to use for automatic completion (e.g. for i18n). I enhanced Noel's work so that it's even possible to use more than one string per item. I created a small demo for this feature...

https://jdnc-incubator.dev.java.net/documentation/bierhance/AirportDemo....

You can select the airports in the demo using either their names or - in case you're having trouble entering the german umlaute ;-) - their IATA/ICAO codes.

Hope y'all having a nice weekend,
Thomas

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Noel Grandin

Hi

Excellent! Thanks Thomas.

I note that in AutoCompleteDecorator#decorate there are 2 pieces of code
listening to the editor property that appear to be doing very similar
things.

I suspect the following patch might be more correct.

Thanks,
Noel Grandin

Index: AutoCompleteDecorator.java
===================================================================
RCS file:
/cvs/swingx/src/java/org/jdesktop/swingx/autocomplete/AutoCompleteDecorator.java,v
retrieving revision 1.5
diff -u -r1.5 AutoCompleteDecorator.java
--- AutoCompleteDecorator.java 29 Jul 2006 14:57:48 -0000 1.5
+++ AutoCompleteDecorator.java 31 Jul 2006 08:18:14 -0000
@@ -140,10 +140,10 @@
comboBox.setEditable(true);

// configure the text component=editor component
- JTextComponent editor = (JTextComponent)
comboBox.getEditor().getEditorComponent();
+ JTextComponent editorComponent = (JTextComponent)
comboBox.getEditor().getEditorComponent();
final AbstractAutoCompleteAdaptor adaptor = new
ComboBoxAdaptor(comboBox);
final AutoCompleteDocument document = new
AutoCompleteDocument(adaptor, strictMatching, stringConverter);
- decorate(editor, document, adaptor);
+ decorate(editorComponent, document, adaptor);

// show the popup list when the user presses a key
final KeyListener keyListener = new KeyAdapter() {
@@ -161,35 +161,31 @@
}
}
};
- editor.addKeyListener(keyListener);
+ editorComponent.addKeyListener(keyListener);

if
(stringConverter!=ObjectToStringConverter.DEFAULT_IMPLEMENTATION) {
comboBox.setEditor(new
AutoCompleteComboBoxEditor(comboBox.getEditor(), stringConverter));
}

- comboBox.addPropertyChangeListener("editor", new
PropertyChangeListener() {
- public void propertyChange(PropertyChangeEvent e) {
- if (!(e.getNewValue() instanceof
AutoCompleteComboBoxEditor)) {
- comboBox.setEditor(new
AutoCompleteComboBoxEditor((ComboBoxEditor) e.getNewValue(),
stringConverter));
- }
- }
- });
-
// Changing the l&f can change the combobox' editor which in turn
// would not be autocompletion-enabled. The new editor needs to
be set-up.
- comboBox.addPropertyChangeListener(new PropertyChangeListener() {
+ comboBox.addPropertyChangeListener("editor", new
PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
- if (e.getPropertyName().equals("editor")) {
- ComboBoxEditor editor = comboBox.getEditor();
- if (editor!=null &&
editor.getEditorComponent()!=null) {
- if
(stringConverter!=ObjectToStringConverter.DEFAULT_IMPLEMENTATION) {
- editor = new
AutoCompleteComboBoxEditor(editor, stringConverter);
- comboBox.setEditor(editor);
- }
- decorate((JTextComponent)
editor.getEditorComponent(), document, adaptor);
-
editor.getEditorComponent().addKeyListener(keyListener);
- }
- }
+ ComboBoxEditor editor = (ComboBoxEditor) e.getNewValue();
+ if (editor!=null && editor.getEditorComponent()!=null)
+ {
+ if (!(editor instanceof AutoCompleteComboBoxEditor)
+ &&
stringConverter!=ObjectToStringConverter.DEFAULT_IMPLEMENTATION)
+ {
+ comboBox.setEditor(new
AutoCompleteComboBoxEditor(editor, stringConverter));
+ // Note: I don't do the decorate step here
because calling setEditor will trigger
+ // the propertychange listener a second time, which
will do the decorate and addKeyListener
+ // step.
+ } else {
+ decorate((JTextComponent)
editor.getEditorComponent(), document, adaptor);
+
editor.getEditorComponent().addKeyListener(keyListener);
+ }
+ }
}
});
}
jdnc-interest@javadesktop.org wrote:
> Hello!
>
> Today I finally found some time to work on our autocomplete code.
>
> I incorporated a patch that was originally submitted by Noel Grandin to decouple the autocomplete feature from the items' toString method (sorry Noel for not being more responsive...).
>
> It's now possible to supply a converter object that determines the string to use for automatic completion (e.g. for i18n). I enhanced Noel's work so that it's even possible to use more than one string per item. I created a small demo for this feature...
>
> https://jdnc-incubator.dev.java.net/documentation/bierhance/AirportDemo....
>
> You can select the airports in the demo using either their names or - in case you're having trouble entering the german umlaute ;-) - their IATA/ICAO codes.
>
> Hope y'all having a nice weekend,
> Thomas
> [Message sent by forum member 'bierhance' (bierhance)]
>
> http://forums.java.net/jive/thread.jspa?messageID=137912
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
> For additional commands, e-mail: jdnc-help@jdnc.dev.java.net
>
>
>

NOTICE: This email, and the contents thereof,
are subject to the standard Peralex email disclaimer, which may
be found at: http://www.peralex.com/disclaimer.html

If you cannot access the disclaimer through the URL attached
and you wish to receive a copy thereof please send
an email to email@peralex.com

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

sbusch
Offline
Joined: 2004-10-05

This clearly is the wrong place to ask - but what the heck: This crashes my machine (ctrl-alt-del sometimes doesn't work!) right after choosing a combo box item. I'm 99.99% sure it's because I have an ATI 9800 video card, but what's interesting is that I've been using swingx for quite awhile w/ no problems, so I'd love to know what's done differently here. If I could narrow it down, perhaps once and for all Sun/ATI/whomever could fix this ...

Thanks.