Question

I'm looking for a Combo(Viewer) in SWT/JFace which supports autocomplete / type-ahead, i.e. the user can enter a couple of characters and the drop down list should show all matching elements.

Was it helpful?

Solution

You can also check out the org.eclipse.jface.fieldassist.AutoCompleteField class. It's not a combo, just a text field, but it adds auto complete functionality as if it were a combo very easily. You can do something as simple as this:

Text textField = new Text(parentComposite, SWT.BORDER);
new AutoCompleteField(textField, new TextContentAdapter(), new String[] 
    {"autocomplete option 1", "autocomplete option 2"});

OTHER TIPS

I don't think there is anything like this built into either Combo or ComboViewer.

As thehiatus suggests org.eclipse.jface.fieldassist.AutoCompleteField is probably the best place to look for this, however, there is support for Combos:

new AutoCompleteField(combo, new ComboContentAdapter(), new String[] 
    {"item0", "item1"});

You may be interested in Eclipse's "Content Assist" feature. You can see it in action when using the Eclipse IDE's Java editor. As you edit source code, you will sometimes see a drop-down menu with phrases that complete what you were typing. (Note that you can press Ctrl+Space to force the drop-down menu to be displayed.)

You can implement this in your own SWT/JFace application as well. The "Java Developer's Guide to Eclipse" has an sample application that implements Content Assist. The sample application is a SQL editor, and it is described in Chapter 26, "Building a Custom Text Editor with JFace Text." There's actually an online overview of the chapter here. The sample SQL editor project, com.ibm.jdg2e.editor.jfacetext.sql, can be found here.

On the other hand, if you want to create your own Combo widget and auto-populate it based on input that is being entered, then this might not be very applicable. I'm thinking the org.eclipse.jface.viewers.ComboViewer might be helpful (though I'm not positive).

Check out: http://sourceforge.net/projects/swtaddons/

I use it in my project (with a little tweak).

It's really dead easy to set this up.

As thanks to paz117's comment, thought I'd share the code to make this work:

String[] proposals = new String[controller.model().size()];

for (int i = 0; i < controller.model().size(); i++) 
    proposals[i] = controller.model().get(i).getAppropriateName();

comboViewer = new ComboViewer(parent, SWT.NONE);
comboViewer.setContentProvider(new ArrayContentProvider());
comboViewer.setLabelProvider(new AppropriateLabelProvider());
comboViewer.setInput(_controller.model());
// additionally, configure the comboViewer arbitrary
new AutoCompleteField(comboViewer.getCombo(), new ComboContentAdapter(), proposals);

The only minor nuisance is that you have to separately populate the model of ComboViewer and AutoCompleteField separately, but that can be at least automated via a static utility method or something similar.

As reference for future visitors, the AutocompleteComboInput (SWT Add-on), can also be a way to achieve this.

AutocompleteComboInput

Code snippet for screenshot (refer to documentation link above for the code template):

import net.sf.swtaddons.autocomplete.combo.AutocompleteComboInput;
...

subjectCodeCombo = new Combo(tab3Composite, SWT.DROP_DOWN);
// other code modifying Combo appearance here...

// returns a String[] of items retrieved from database
String[] subjectCodeArray = dbQuery.subjectsToArray();

subjectCodeCombo.setItems(subjectCodeArray);
subjectCodeCombo.setText("- SELECT -");
new AutocompleteComboInput(subjectCodeCombo);

The add-on requires all JARs below to be added to the Library: (more info)

  • eclipse-equinox-common-3.5.0.jar
  • net.sf.swtaddons_0.1.1_bin_src.jar (sourceforge)
  • org.eclipse.core.commands.jar
  • org.eclipse.jface-3.6.0.jar

Click here for JAR pack.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top