gpt4 book ai didi

java - Jface ContentProposalAdapter 不适用于第一个输入的字符

转载 作者:行者123 更新时间:2023-12-01 14:34:55 24 4
gpt4 key购买 nike

我正在尝试为我在 SWT/JFace 应用程序中使用的文本小部件进行字段辅助。

我在 Text 组件上添加了一个 ModifyListener,当它被 ModifyEvent 触发时,此处的 setAutoCompletion() 方法和下一个方法将被调用:

问题是它只有在输入至少两个字符后才有效。这意味着如果我有“387”作为建议,我必须输入“3”,然后输入“8”。弹出窗口仅显示第二个“8”字符。

之后,它总是按预期工作,但我不知道为什么文本第一次收到事件时它不起作用。我一直在“stackoverflow”和 Google 中查找,但没有找到任何内容。

    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;
}

最佳答案

首先感谢您的代码。我一直在寻找这样的东西并且我已经实现了它。当我这样做的时候,我遇到了和你完全相同的问题。我猜它与修改事件行为有关。我只是将其更改为在 keyPressed 事件上启动,现在它工作正常。

关于java - Jface ContentProposalAdapter 不适用于第一个输入的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16570195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com