使用键盘滚动弹出列表时如何使 JComboBox 选定的项目不更改
我在面板中有一个 JComboBox 组件,并附加了 ItemListener。但每次按下向上/向下按键后(滚动打开的弹出列表时)它都会被触发。我想在用户通过按 Enter 键接受选择后更改所选值。
使用鼠标时则不会出现这种情况。当我将鼠标移到组合框的列表上时,突出显示会跟随鼠标指针,但在按下鼠标按钮之前,所选项目不会更改。我希望键盘具有相同的行为,即通过向上/向下箭头移动突出显示不会更改所选项目,但按 Enter 会更改。
I have a JComboBox component in the panel and ItemListener attached to it. But it gets fired after every up/down keypress (when scrolling though opened popup list). I want to change the selected value after the user accepts selection by pressing for example Enter key.
This is not a case when using mouse. When I move mouse over the combobox's list the highlight follows mouse pointer, but selected item is not changed until I press the mouse button. I would like to have the same behavior for keyboard, i.e. moving highlight via up/down arrow does not change selected item, but pressing Enter does.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信您应该能够执行以下操作:
在将
comboBox
实例创建到 获取此功能I believe you should be able to do:
after you have created your
comboBox
instance to get this functionality在 Java 8 中,他们修复了这一行为,但只有在您设置一个 UI 属性时才会触发
In Java 8 they have fixed this behaviour, but only trigger if u set one UI property
JComboBox.isTableCellEditor
方法适用于列表中的箭头移动,但不适用于 KeySelectionManager 支持的预输入。即,您仍然会为用户键入的每个非导航键获取 ActionEvents,因为 JComboBox 解释这些字符以通过模型进行搜索以移动到(或靠近)用户的预期选择。这个解决方案有一个缺点,它改变了鼠标点击的操作命令,这对我来说是一个不错的妥协,因为 GUI 的流程迫使用户将焦点从组合框上移开,
我最终制作了一个特殊的 KeyListener,它依赖于将组合框的默认操作命令从
comboBoxChanged
更改为comboBoxMovement
。这是组合框全部初始化后我需要的代码行:...这是完成所有工作的方法及其包含的类:
...这是执行操作的方法
the
JComboBox.isTableCellEditor
method works for arrow movement through the list, but does not work for type-ahead supported by the KeySelectionManager. i.e. you still get ActionEvents for every non-navigation key the user types, as the JComboBox interprets those characters for searching though the model to move to (or move close to) the user's intended selection.this solution has a drawback in that it changes the action command for mouse clicks, which was a OK compromise for me because the the flow of the GUI forces the user to change the focus away from the combo box
I ended up making a special KeyListener, that relys on changing the combo box's default action command from
comboBoxChanged
tocomboBoxMovement
. Here's the line of code I need after my combo box is all initialized:... and here is the method and its contained class that do all the work:
... and here's the action performed method
这是
ItemListener
的预期行为。每当显示的值发生变化时,就会触发该事件。根据您的要求,请使用ActionListener
。Its the expected behavior with the
ItemListener
. whenever the displayed value changes the event is fired. For your requirement use anActionListener
.