Java ComboViewer 对编辑做出反应,KeyListener?

发布于 2024-12-21 00:21:18 字数 771 浏览 0 评论 0原文

我在插件中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑依然 2024-12-28 00:21:19

您应该使用 ModifyListener 来代替并查询组合文本框的当前值,如下所示:

public void modifyText(ModifyEvent e)
{
  String text = ((Combo) e.getSource()).getText();
  ...
}

但这只会为您提供文本框的内容。据我记得,组合查看器不会有任何选择,直到您从其下拉列表中选择一个元素或使用 setSelection(..) 以编程方式设置一个元素。可见文本基本上只是标签(就组合查看器而言),并且由于标签不必是唯一的,因此它不会尝试查找属于该标签的模型元素。因此,我不知道当有人在组合的文本框中键入内容时,您期望组合查看器的选择是什么。您究竟想实现什么样的行为?

You should use a ModifyListener instead and query the current value of the combo's text box like this:

public void modifyText(ModifyEvent e)
{
  String text = ((Combo) e.getSource()).getText();
  ...
}

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?

此生挚爱伱 2024-12-28 00:21:19

内容辅助不是更有帮助吗?它的工作方式与 Eclipse 中的内容辅助完全相同。

Won't a content assist be more helpfull? It works exactly like the content assist in Eclipse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文