Java Jface ContentProposalAdapter无法处理第一个键入的字符
我试图为我在SWT/JFace应用程序中使用的文本小部件创建字段辅助 我在文本组件上添加了ModifyListener,当ModifyEvent触发此操作时,将调用下面的setAutoCompletion()方法和下一个方法: 问题是,它只有在输入至少两个字符后才能工作。这意味着,如果我有“387”作为建议,我必须键入“3”,然后键入“8”。弹出窗口仅显示第二个“8”字符 在那之后,它总是像预期的那样工作,但是我不知道为什么文本第一次收到事件时它不工作。我一直在搜索“stackoverflow”和谷歌,但没有找到任何东西Java Jface ContentProposalAdapter无法处理第一个键入的字符,java,events,event-handling,swt,jface,Java,Events,Event Handling,Swt,Jface,我试图为我在SWT/JFace应用程序中使用的文本小部件创建字段辅助 我在文本组件上添加了ModifyListener,当ModifyEvent触发此操作时,将调用下面的setAutoCompletion()方法和下一个方法: 问题是,它只有在输入至少两个字符后才能工作。这意味着,如果我有“387”作为建议,我必须键入“3”,然后键入“8”。弹出窗口仅显示第二个“8”字符 在那之后,它总是像预期的那样工作,但是我不知道为什么文本第一次收到事件时它不工作。我一直在搜索“stackoverflow”
private void setAutoCompletion(final Widget widget, final String value) {
try {
LOG.debug("Llamada desde " + widget.toString());
ContentProposalAdapter adapter = null;
final String[] proposals = getAllProposals(widget, value);
LOG.debug("Las sugerencias para el widget son: " + proposals);
for (final String s : proposals) {
LOG.debug(s);
}
final SimpleContentProposalProvider scp = new SimpleContentProposalProvider(proposals);
scp.setProposals(proposals);
scp.setFiltering(true);
if (widget instanceof Text) {
adapter = new ContentProposalAdapter((Text) widget, new TextContentAdapter(), scp, null, null);
} else {
adapter = new ContentProposalAdapter((Combo) widget, new ComboContentAdapter(), scp, null, null);
}
adapter.setEnabled(true);
adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
} catch (final Exception e) {
MessagePanel.openError(Display.getCurrent().getActiveShell(), GUITexts
.get(Labels.DESTOCK_PROPOSAL_ERROR_TITLE), GUITexts.get(Labels.DESTOCK_PROPOSAL_ERROR_TEXT));
}
}
private String[] getAllProposals(final Widget widget, final String text) {
List<String> proposals = new ArrayList<String>();
if (text == null || text.length() == 0) {
proposals = null;
} else {
if (widget instanceof Text) {
for (final Workorder wo : this.openWorkorders) {
if (wo.getWorkorderId().toString().startsWith(text)) {
proposals.add(wo.getWorkorderId().toString());
}
}
} else if (widget instanceof Combo) {
for (final Workorder wo : this.openWorkorders) {
if (wo.getDescription().startsWith(text)) {
proposals.add(wo.getDescription());
}
}
}
}
String[] result = null;
if (proposals != null) {
result = new String[proposals.size()];
for (int i = 0; i < result.length; i++) {
result[i] = proposals.get(i);
}
} else {
result = new String[0];
}
return result;
}
private void setAutoCompletion(最终小部件,最终字符串值){
试一试{
LOG.debug(“Llamada desde”+widget.toString());
ContentProposalAdapter适配器=null;
最终字符串[]Propositions=GetAllPropositions(小部件,值);
LOG.debug(“Las sugerencias para el widget son:”+提案);
用于(最终字符串s:提案){
日志调试;
}
最终SimpleContentProposalProvider scp=新SimpleContentProposalProvider(建议书);
scp.SETP建议书(建议书);
scp.setFiltering(真);
if(小部件实例of Text){
adapter=newcontentproposaladapter((Text)小部件,newtextcontentadapter(),scp,null,null);
}否则{
adapter=new ContentProposalAdapter((组合)小部件,new ComboContentAdapter(),scp,null,null);
}
adapter.setEnabled(true);
adapter.setProposalAcceptanceStyle(内容ProposalAdapter.PROPOSAL\u替换);
}捕获(最终异常e){
MessagePanel.openError(Display.getCurrent().getActiveShell(),GUITexts
.get(Labels.DESTOCK\u PROPOSAL\u ERROR\u TITLE)、GUITexts.get(Labels.DESTOCK\u PROPOSAL\u ERROR\u TEXT));
}
}
私有字符串[]GetAllPropositions(最终小部件小部件,最终字符串文本){
列表建议=新建ArrayList();
if(text==null | | text.length()==0){
提议=无效;
}否则{
if(小部件实例of Text){
对于(最终工单wo:此.打开工单){
if(wo.getWorkorderId().toString().startsWith(text)){
添加(wo.getWorkorderId().toString());
}
}
}else if(组合的小部件实例){
对于(最终工单wo:此.打开工单){
if(wo.getDescription().startsWith(text)){
添加(wo.getDescription());
}
}
}
}
字符串[]结果=null;
如果(建议!=null){
结果=新字符串[Propositions.size()];
for(int i=0;i
首先感谢您的代码。我一直在寻找这样的东西,我已经实现了。
当我这么做的时候,我和你有着完全相同的问题。我猜这与ModifyEvent行为有关。我只是简单地将其更改为在按键事件时启动,现在它工作得很好。我不知道这是否与此链接中的代码有关:其中的内容为://Work around for Field assistant仅在键入//第二个字符后激活-STARTI已测试过作为URL示例执行,但不起作用。我还检查了对侦听器的调用,它确实起作用(它被调用并运行)。只是ContentProposalAdapter在第一次输入时没有被激活。你解决了吗?我也有同样的问题@这是几年前的事了。我现在没有代码,无法测试它,但请尝试使用Othmane的答案。