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.
我正在寻找SWT/JFace中的一个组合(查看器),它支持自动完成/提前输入,即用户可以输入几个字符,下拉列表应该显示所有匹配的元素。
6 个解决方案
#1
11
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:
您还可以检查org. eclipsee .jface.fieldassist。AutoCompleteField类。它不是一个组合,只是一个文本字段,但它添加了自动完成功能,就好像它是一个组合非常容易。你可以做一些简单的事情:
Text textField = new Text(parentComposite, SWT.BORDER);
new AutoCompleteField(textField, new TextContentAdapter(), new String[]
{"autocomplete option 1", "autocomplete option 2"});
#2
6
I don't think there is anything like this built into either Combo or ComboViewer.
我不认为组合或ComboViewer里面有任何类似的东西。
As thehiatus suggests org.eclipse.jface.fieldassist.AutoCompleteField
is probably the best place to look for this, however, there is support for Combos:
作为thehiatus表明org.eclipse.jface.fieldassist。AutoCompleteField可能是寻找这个的最好地方,然而,有对Combos的支持:
new AutoCompleteField(combo, new ComboContentAdapter(), new String[]
{"item0", "item1"});
#3
3
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.)
您可能对Eclipse的“内容辅助”特性感兴趣。在使用Eclipse IDE的Java编辑器时,您可以看到它的作用。当您编辑源代码时,您有时会看到一个下拉菜单,其中有完整的您正在键入的短语。(注意,您可以按Ctrl+Space来强制显示下拉菜单。)
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.
您也可以在自己的SWT/JFace应用程序中实现这一点。“Java开发人员的Eclipse指南”有一个实现内容帮助的示例应用程序。示例应用程序是一个SQL编辑器,在第26章“使用JFace文本构建自定义文本编辑器”中进行了描述。这里有一个关于这一章的在线概述。示例SQL编辑器项目,com.ibm.jdg2e.editor.jfacetext。sql,可以在这里找到。
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).
另一方面,如果您想创建自己的组合小部件并根据输入自动填充它,那么这可能不是非常适用。我想org.eclipse.jface.viewers。ComboViewer可能有用(虽然我不是很肯定)。
#4
1
Check out: http://sourceforge.net/projects/swtaddons/
查阅:http://sourceforge.net/projects/swtaddons/
I use it in my project (with a little tweak).
我在我的项目中使用它(稍微调整一下)。
#5
1
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:
感谢paz117的评论,我想我应该分享这段代码:
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.
唯一的小麻烦是您必须分别填充ComboViewer和AutoCompleteField模型,但这至少可以通过静态实用程序方法或类似的方法自动完成。
#6
1
As reference for future visitors, the AutocompleteComboInput (SWT Add-on), can also be a way to achieve this.
作为未来访问者的参考,AutocompleteComboInput (SWT附加组件)也可以实现这一点。
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)
该附加组件要求将下面的所有jar添加到库中:(更多信息)
- eclipse-equinox-common-3.5.0.jar
- eclipse-equinox-common-3.5.0.jar
- net.sf.swtaddons_0.1.1_bin_src.jar (sourceforge)
- net.sf.swtaddons_0.1.1_bin_src。jar(sourceforge)
- org.eclipse.core.commands.jar
- org.eclipse.core.commands.jar
- org.eclipse.jface-3.6.0.jar
- org.eclipse.jface-3.6.0.jar
Click here for JAR pack.
点击这里获取JAR包。
#1
11
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:
您还可以检查org. eclipsee .jface.fieldassist。AutoCompleteField类。它不是一个组合,只是一个文本字段,但它添加了自动完成功能,就好像它是一个组合非常容易。你可以做一些简单的事情:
Text textField = new Text(parentComposite, SWT.BORDER);
new AutoCompleteField(textField, new TextContentAdapter(), new String[]
{"autocomplete option 1", "autocomplete option 2"});
#2
6
I don't think there is anything like this built into either Combo or ComboViewer.
我不认为组合或ComboViewer里面有任何类似的东西。
As thehiatus suggests org.eclipse.jface.fieldassist.AutoCompleteField
is probably the best place to look for this, however, there is support for Combos:
作为thehiatus表明org.eclipse.jface.fieldassist。AutoCompleteField可能是寻找这个的最好地方,然而,有对Combos的支持:
new AutoCompleteField(combo, new ComboContentAdapter(), new String[]
{"item0", "item1"});
#3
3
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.)
您可能对Eclipse的“内容辅助”特性感兴趣。在使用Eclipse IDE的Java编辑器时,您可以看到它的作用。当您编辑源代码时,您有时会看到一个下拉菜单,其中有完整的您正在键入的短语。(注意,您可以按Ctrl+Space来强制显示下拉菜单。)
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.
您也可以在自己的SWT/JFace应用程序中实现这一点。“Java开发人员的Eclipse指南”有一个实现内容帮助的示例应用程序。示例应用程序是一个SQL编辑器,在第26章“使用JFace文本构建自定义文本编辑器”中进行了描述。这里有一个关于这一章的在线概述。示例SQL编辑器项目,com.ibm.jdg2e.editor.jfacetext。sql,可以在这里找到。
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).
另一方面,如果您想创建自己的组合小部件并根据输入自动填充它,那么这可能不是非常适用。我想org.eclipse.jface.viewers。ComboViewer可能有用(虽然我不是很肯定)。
#4
1
Check out: http://sourceforge.net/projects/swtaddons/
查阅:http://sourceforge.net/projects/swtaddons/
I use it in my project (with a little tweak).
我在我的项目中使用它(稍微调整一下)。
#5
1
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:
感谢paz117的评论,我想我应该分享这段代码:
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.
唯一的小麻烦是您必须分别填充ComboViewer和AutoCompleteField模型,但这至少可以通过静态实用程序方法或类似的方法自动完成。
#6
1
As reference for future visitors, the AutocompleteComboInput (SWT Add-on), can also be a way to achieve this.
作为未来访问者的参考,AutocompleteComboInput (SWT附加组件)也可以实现这一点。
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)
该附加组件要求将下面的所有jar添加到库中:(更多信息)
- eclipse-equinox-common-3.5.0.jar
- eclipse-equinox-common-3.5.0.jar
- net.sf.swtaddons_0.1.1_bin_src.jar (sourceforge)
- net.sf.swtaddons_0.1.1_bin_src。jar(sourceforge)
- org.eclipse.core.commands.jar
- org.eclipse.core.commands.jar
- org.eclipse.jface-3.6.0.jar
- org.eclipse.jface-3.6.0.jar
Click here for JAR pack.
点击这里获取JAR包。