JCombox setSelectedItem() 导致 JTable 失去焦点

发布于 2024-12-04 12:51:21 字数 122 浏览 2 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(1

春风十里 2024-12-11 12:51:21

在您的情况下,您不应使用 JCombobox#setSelectedItem。 此方法调用将引发 ActionEvent,这可能会导致焦点转到下一个组件。尝试使用

JCombobox combo = ...
combo.getModel().setSelectedItem(...);

更新: 我查看了实现的代码,看来使用 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 instead

JCombobox combo = ...
combo.getModel().setSelectedItem(...);

Update: 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.

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