JCombox setSelectedItem() 导致 JTable 失去焦点
我正在 JTable 中实现自动完成组合框,一切似乎都工作正常,但是当我触发 model.setSelectedItem(item); 来选择组合中的当前项目时-list,组合框失去焦点。
我们可以重写这个函数吗?
I was implementing a auto completion combobox in a JTable
, everything seems to work fine, but when I fire model.setSelectedItem(item);
to select the current Item in the combo-list, the combobox loses focus.
Can we override this function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,您不应使用 JCombobox#setSelectedItem。 此方法调用将引发 ActionEvent,这可能会导致焦点转到下一个组件。尝试使用
更新: 我查看了实现的代码,看来使用
JComboboBox#setSelectedIndex
正是您正在寻找的。当您键入字符并且列表显示以键入的字符开头的下一项时,也会使用此方法。In your case you shouldn't use
JCombobox#setSelectedItem.
This method call will raise an ActionEvent which might cause the focus to go over to the next component. Try to use insteadUpdate: I reviewed the implementation's code and it seems that using
JComboboBox#setSelectedIndex
is what you are looking for. This method is also used when you type a character and the list shows the next item starting with the typed character.