简体   繁体   English

如何在JComboBox中的enter上触发Java Swing InputVerifier(actionPerformed)?

[英]How to trigger Java Swing InputVerifier on enter in JComboBox (actionPerformed)?

I have a Swing JComboBox with an InputVerifier set correctly. 我有一个正确设置了InputVerifier的Swing JComboBox

I am using the combo box to set an integer. 我正在使用组合框设置一个整数。

If I type "cat" in the field and hit tab, my InputVerifier triggers and resets the value to "0". 如果我在该字段中键入“ cat”并单击选项卡,则我的InputVerifier会触发并将该值重置为“ 0”。

If I type "cat" and hit enter, my InputVerifier is never called from actionPerformed . 如果我输入“ cat”并按Enter,则从不会从actionPerformed调用我的InputVerifier Do I need to explicitly call my InputVerifier from actionPerformed ? 我需要从actionPerformed显式调用InputVerifier吗?

What's the best model to validate my JComboBox on tab and enter? 验证我的JComboBox在选项卡上并输入)的最佳模型是什么? It seems like this is something that should be given to me "for free" by the swing model. 似乎这是秋千模型应该“免费”给予我的东西。

The problem is "hit Tab" and "hit Enter" mean two different things in Java Swing. 问题是“命中Tab”和“命中Enter”在Java Swing中意味着两个不同的东西。 But those two actions mean the same thing to you, me, and the user. 但是,这两个动作对您,我和用户都意味着同一件事。

Swing has no single mechanism to detect "when the user is done entering data". Swing没有单一机制来检测“当用户完成数据输入时”。 Instead, Swing focuses on the mechanics of "is this field losing keyboard focus" and "is the user pressing Enter key while inside a field". 相反,Swing专注于“此字段是否失去键盘焦点”和“用户是否在字段内按下Enter键”的机制。

Semantically those two actions mean the same thing from the user's perspective: "I'm done. Here's my input.". 从用户的角度来看,这两个动作在语义上意味着相同的事情:“我完成了。这是我的输入。” But, from what I can tell, Swing fails to offer a way to detect that user intention. 但是,据我所知,Swing无法提供一种检测用户意图的方法。 I'm as surprised as you by the lack of such a feature, as this seems to be the most basic function of a form in a GUI. 缺少这种功能令我感到惊讶,因为这似乎是GUI中表单的最基本功能。 What we need, but don't have, is a "dataEntered" event. 我们需要但没有的是一个“ dataEntered”事件。

There is a workaround… 有一种解决方法...

In a similar context (JTextField instead of JComboBox) the Sun/Oracle Java Tutorial provides the example InputVerificationDemo where a class is created that: 在类似的上下文中(用JTextField代替JComboBox),Sun / Oracle Java教程提供了示例InputVerificationDemo ,其中创建了一个类,该类:

  • Extends InputVerifier (to handle tabbing/clicking where focus is about to be lost) 扩展InputVerifier (以处理将要失去焦点的选项卡/单击)
  • Implements ActionListener (to handle pressing Enter key without leaving field) 实现ActionListener (处理Enter键而不离开字段)

The good thing about this workaround is that you can locate your handling code all in one place. 这种解决方法的好处是,您可以将处理代码全部放在一个地方。 The downside is that you still have the hassle of: 缺点是您仍然有以下麻烦:

This is the expected behavior of InputVerifier : the TAB key attempts to change focus, while the ENTER key does not. 这是InputVerifier的预期行为: TAB键尝试更改焦点,而ENTER键则没有。 You can bind the ENTER key to a different action, as described in the tutorial How to Use Key Bindings . 您可以将ENTER键绑定到其他操作,如教程如何使用键绑定中所述 Also, consider the informative article Key Bindings , which includes a handy utility application. 另外,请参考内容丰富的文章Key Bindings ,其中包括一个方便的实用程序。

When using an editable combo box, focus is on a JTextField which is used as the editor of the combo box. 当使用可编辑的组合框时,焦点位于用作组合框编辑器的JTextField上。 You can add an ActionListener to this text field. 您可以将ActionListener添加到此文本字段。

In the ActionListener you could try invoking the transferFocus() method which should be equivalent to tabbing our of the text field. 在ActionListener中,您可以尝试调用transferFocus()方法,该方法应该等效于在文本字段中制表符。 If that doesn't work then tha actionListener should invoke the same editing code as the InputVerifier. 如果这不起作用,则actionListener应调用与InputVerifier相同的编辑代码。

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

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