Skip to main content

AutoComplete in JComboBox with custom renderer

4 replies [Last post]
rafaelsd
Offline
Joined: 2008-01-17

hi ppl, this is my first time asking something here.

I have a problem in the application that i'm developing. I have sothing like: a table, with various things, and i have a filters panel. Everything works very good. Now what i want to do is to add the autocomplete option to the jcombobox that i have there.
The problem is how to do it.

i have 4 different renderers for each of the 4 combos. In my code i have it like this sequence:

prepare lazy filters
then set the renders for each one:

setComboRenderers(rcCombo, prCombo, usrCombo, pyrCombo);

....

private void setComboRenderers(JComboBox rcCombo, JComboBox prCombo, JComboBox usrCombo, JComboBox pyrCombo) {
rcCombo.setRenderer(new ResponsabilityCenterListRenderer());
prCombo.setRenderer(new ProcessListRenderer());
usrCombo.setRenderer(new UserListRenderer());
if(pyrCombo != null) {
pyrCombo.setRenderer(new PayerListRenderer());
}
}

and adding the listeners for each combo i call

fillComboFilter(cb, rcService.findAll(), true);

....

public void fillComboFilter(JComboBox comboBox, Collection values, boolean emptyItem) {
if(emptyItem) {
comboBox.addItem(null);
}

if(values != null) {
for(Object obj : values) {
comboBox.addItem(obj);
}
}
}

Now for do it i tried in the last piece of code that i posted, change it like this first:

public void fillComboFilter(JComboBox comboBox, Collection values, boolean emptyItem) {
if(emptyItem) {
comboBox.addItem(null);
}

if(values != null) {
for(Object obj : values) {
comboBox.addItem(obj);
}
}

AutoCompleteDecorator.decorate(comboBox);
}

But like this the names that appear for autocomplete aren't the correct items but something like the reference to that object.

if i try to do it like:

public void fillComboFilter(JComboBox comboBox, Collection values, boolean emptyItem) {
if(emptyItem) {
comboBox.addItem(null);
}

if(values != null) {
for(Object obj : values) {
comboBox.addItem(obj);
}
}

AutoCompleteDecorator.decorate(comboBox, new ObjectToStringConverter() {
public String getPreferredStringForItem(Object item) {
if(!(item instanceof ResponsabilityCenter) || item == null) {
return null; }
return ((ResponsabilityCenter) item).getAbbreviation();
}
});

}

i'll run into problems because, it is not the best code, and i'll have to find some way around to make it work for each different combobox.

One of the renderers i have is something like this:

public class ResponsabilityCenterListRenderer extends JLabel implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if(value != null && value instanceof ResponsabilityCenter) {
setText(((ResponsabilityCenter) value).getAbbreviation());
} else {
setText(" ");
}
return this;
}

}

the others follow the same scheme.

So i ask for your help to find a good solution for this problem...

Thanks for any answer.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rafaelsd
Offline
Joined: 2008-01-17

so, i guess there's not a way to do it....

so, does anyone knows other way to make a jlist into a combo without the renderers so i can make with autocomplete option?

kirillcool
Offline
Joined: 2004-11-17

Have you seen [1]? It has examples for combo boxes with custom model, renderer and editor for SwingX (as well as for other alternatives).

[1] http://today.java.net/pub/a/today/2007/07/19/adding-auto-completion-to-s...

rafaelsd
Offline
Joined: 2008-01-17

sure, i was following that page and coding like it suggested, but i don't know, it seems i doesn't work as i would expect it to...

the main problem is that in my specific case it becomes a "complex code". I mean, i set the render and again when i add the autocompletedecorator.decorate(comboBox), i have to set up again an objecttostring option because it seems to disable the render.

anyway, thanks for the answer.

kirillcool
Offline
Joined: 2004-11-17

It has a complete example - does that work for you? If it doesn't, then your expectations do not match how the functionality works. If it does, then start rolling back your changes until you get to the one that broke the auto-completion.