Java ComboViewer 对编辑做出反应,KeyListener?
我在插件中使用 JFace ComboViewer。作为一项便利功能,我想通过开始输入来更改当前选择。 为此,我向底层 Combo 元素添加了一个 KeyListener,
当我开始键入时,我得到的是一个事件,但不幸的是,我没有得到更改后的值,而只得到开始键入之前的值。
我的按键监听器(引用了 ComboViewer)当前以这种方式做出反应:
@Override
public void keyPressed(KeyEvent e) {
ISelection selection = combo.getSelection(); //combo is the ComboViewer
if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
IStructuredSelection strucSel = (IStructuredSelection) selection;
node = (TreeNode) strucSel.getFirstElement();
...}
我输入的第一个字符实际上通过了 if 语句,任何进一步输入的字符都会导致选择为空。
那么我如何始终获得“当前选择”以及如何获得更改后的值,似乎在按键侦听器中做出反应为时过早,因为我正在输入的文本字段尚未更新,因此将始终提供旧的信息?
也许我理解有误,但我无法想象我会如此困难地从文本字段获取最新信息。 有什么建议吗?
I am using a JFace ComboViewer in my Plugin. As a convenience feature I'd like to change the current selection just by starting typing.
For that I added a KeyListener to the underlying Combo Element
What I get is an event as soon as I start typing, but unfortunately I do not get the changed value, but only the one before I started typing.
My keylistener, which has a reference tot he ComboViewer reacts currently in this way:
@Override
public void keyPressed(KeyEvent e) {
ISelection selection = combo.getSelection(); //combo is the ComboViewer
if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
IStructuredSelection strucSel = (IStructuredSelection) selection;
node = (TreeNode) strucSel.getFirstElement();
...}
The first character I type actually makes it through the if-statement, any further typed character causes the selection to be empty.
So how do I get always a "current selection" and how can I obtain the changed value, it seems like reacting in the keylistener is to early, because the text-field I am typing in is not yet updated and thus will always provide old information?
Maybe I understood something wrong, but I could not imagine that I could be so hard getting an up-to-date information from a text field.
Any advices?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
ModifyListener
来代替并查询组合文本框的当前值,如下所示:但这只会为您提供文本框的内容。据我记得,组合查看器不会有任何选择,直到您从其下拉列表中选择一个元素或使用
setSelection(..)
以编程方式设置一个元素。可见文本基本上只是标签(就组合查看器而言),并且由于标签不必是唯一的,因此它不会尝试查找属于该标签的模型元素。因此,我不知道当有人在组合的文本框中键入内容时,您期望组合查看器的选择是什么。您究竟想实现什么样的行为?You should use a
ModifyListener
instead and query the current value of the combo's text box like this:But this will only give you the content of the text box. The combo viewer won't have - as far as I remember - any selection until you select an element from its drop down list or you programmatically set one using
setSelection(..)
. The visible text is basically only the label (as far the the combo viewer is concerned) and since labels don't have to be unique, it won't try to find the model element that belongs to the label. So I don't know what you expect the combo viewer's selection to be while someone is typing in the combo's text box. What kind of behaviour exactly are you trying to achieve?内容辅助不是更有帮助吗?它的工作方式与 Eclipse 中的内容辅助完全相同。
Won't a content assist be more helpfull? It works exactly like the content assist in Eclipse.