自动完成装饰覆盖

发布于 2024-12-28 12:04:45 字数 364 浏览 1 评论 0原文

在我当前的项目中,我使用 SwingX 的 AutoCompleteDecorateAutoCompleteDecorator.decorate(jComboBox1);

但是,我想覆盖退格操作。最初,使用 AutoCompleteDecorate.decorate(JComboBox) 时,按退格键会将组合框中的选择内容向左移动,并且不会删除前一个字符。我想实现默认的退格功能(即删除前一个字符),即使我 AutoCompleteDecorate 我的 JComboBox 也是如此。

请帮我解决我的问题。先感谢您。

In my current project, I am using AutoCompleteDecorate from SwingX.
AutoCompleteDecorator.decorate(jComboBox1);

However, I want to override the backspace action. Originally, using AutoCompleteDecorate.decorate(JComboBox), pressing backspace move the selection in combobox to the left and doesn't delete the previous character. I want to implement the default function of backspace (which is to delete previous character) even if I AutoCompleteDecorate my JComboBox.

Please help me solve my problem. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

从此见与不见 2025-01-04 12:04:45

并非微不足道 - 装饰器在实现选择而不是删除方面走了很长的路;-)

首先,您需要定义您想要的行为。然后实现一个按照您的意图执行的操作并将其放置在编辑器的 ActionMap 中:

Action myBackspace = ...
ActionMap map = ((JComponent) decorateCombo.getEditor().getEditorComponent())
     .getActionMap();
map.put("nonstrict-backspace", myBackspace);

这是模糊的,因为我不知道您到底想要什么,最好查看 AutoComplete 的源代码以了解如何实现 myBackspace

<强>编辑

只是为了详细说明一下模糊性:我的第一个想法是简单地重新安装默认的退格键绑定,例如:

InputMap map = ((JComponent) decorateCombo.getEditor().getEditorComponent())
    .getInputMap();
map.put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_BACK_SPACE, 0), 
            DefaultEditorKit.deletePrevCharAction);

这很可能不是预期的:假设插入符位于包含的中间的某个位置中的元素可编辑组合,然后选择从插入符号到末尾的文本,因此 deletePrev 删除所选字符而不是上一个字符。这可能会导致实现自定义操作的方式:首先清除选择,然后删除Prev,然后检查新单词是否在列表中并重新选择(或不在)。不知道要求就很难说。

Not trivial - the decorator goes a long way to implement the select-instead-of-delete ;-)

First you need to define which behaviour you want. Then implement a Action that does as you intend and place it in the editor's ActionMap:

Action myBackspace = ...
ActionMap map = ((JComponent) decorateCombo.getEditor().getEditorComponent())
     .getActionMap();
map.put("nonstrict-backspace", myBackspace);

This is vague because because I can't know what exactly you want, best to look at the source of AutoComplete to get an idea of how to implement myBackspace

Edit

Just to elaborate a bit on the vagueness: my first thought was to simply re-install the default backspace binding like:

InputMap map = ((JComponent) decorateCombo.getEditor().getEditorComponent())
    .getInputMap();
map.put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_BACK_SPACE, 0), 
            DefaultEditorKit.deletePrevCharAction);

that's most probably not what is expected: assuming the caret is somewhere in the middle of a contained element in an editable combo, then the text from the caret to the end is selected, consequently the deletePrev deletes the selected not the prev char. Which might lead the way to implement the custom action: first clear the selection, then deletePrev, then check if the new word is in the list and re-select (or not). Hard to tell without knowing the requirement.

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