简体   繁体   English

如何失去对JComboBox的关注?

[英]How do I lose focus on a JComboBox?

I have a JComboBox with an key listener. 我有一个带有键监听器的JComboBox

When I hit <enter> , I fire off some action, and then I need to to lose focus on the JComboBox ! 当我按下<enter> ,我会触发一些动作, 然后我需要失去对JComboBox关注

To focus on it, I can do JComboBoxObject.grabFocus(); 为了专注JComboBoxObject.grabFocus(); ,我可以做JComboBoxObject.grabFocus();

But doing transferFocus() to get the focus to a next element (I don't care WHERE the focus goes, just away from combo box) does NOT work. 但是,执行transferFocus()将焦点移至下一个元素(我不在乎焦点在哪里,仅从组合框移开)不起作用。

Doing grabFocus() from another combo box works, but seems like a pretty annoying hack to me. 从另一个组合框中执行grabFocus()grabFocus() ,但对我来说似乎是一个非常烦人的hack。 Is there a better solution? 有更好的解决方案吗?

Updated: Starting from this two-combo example , adding either of these lines to the actionPerformed() implementation seems to do what you want. 更新:从这个两个组合的示例开始 ,将这两行添加到actionPerformed()实现中似乎可以完成您想要的。

combo1.transferFocus();
combo2.requestFocusInWindow();

I can suggest you to first use the 我可以建议您先使用

.getNextFocusableComponent() .getNextFocusableComponent()

and then use the 然后使用

.requestFocusInWindow() .requestFocusInWindow()

that means Implementing it like this, 就是说这样实现

JComboBox.getNextFocusableComponent().requestFocusInWindow(); JComboBox.getNextFocusableComponent()。requestFocusInWindow();

One important note is that .getNextFocusableComponent() has become obsolete but it can work really better, you can use it but If you have any other solution, I would prefer not using this. 一个重要的注意事项是.getNextFocusableComponent()已经过时了,但是它确实可以更好地工作,可以使用它,但是如果您有其他解决方案,我宁愿不使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM