views:

1252

answers:

2

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.

+2  A: 

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).

bporter
+1  A: 

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"});
thehiatus