java - JTextField 监听器通知顺序

标签 java swing listener jtextfield

我正在使用 JTextField,其中我过滤通过 REGEX 传入的输入并通知用户(背景颜色更改),然后通过套接字推送更新(逐个字符,而不是“当您输入时按回车键”)根据规范,“完成”行为)

问题是,由于没有保证 DocumentListener 通知的顺序,因此我无法将更新的传输和更新的验证放在同一种监听器上。使用 JTextField 的不同类型的监听器(KeyListener、DocumentListener、...)之间是否有保证的通知顺序?

我找不到有关不同类型监听器之间的有用文档,只能(似乎)在单一类型的监听器(例如 DocumentListener)中找到

谢谢!

编辑 1 验证 - REGEX 验证不允许我禁止任何文本进入,因为附加字符可能会使“无效”字符串“有效”。

行为分离 - 根据设计,我无法将两种行为合并到一个监听器中,它们是在代码流中的不同时间设置的,并且无法更改。这是一个没有争议的协议(protocol)设计问题。如果不解释整个事情,我就无法真正进入更有用的细节。重点是他们只是被分成两个听众。我需要在验证步骤之后进行传输步骤,因此我需要知道是否有 Listener KIND 通知的具体定义。我所说的“种类”是指 KeyListener、DocumentListener 和 UndoableEditListener。我知道在每种监听器中都不能保证顺序。

但是,是否可以保证 DocumentListener 都在 UndoableEditListener 之前得到通知,反之亦然?或者所有不同类型的监听器都只是以任何特定顺序通知。

编辑2

抱歉,我们似乎在杂草硬核中迷失了方向。

此时此刻,我想弄清楚的是:

对于 JTextField 上不同类监听器(例如:Key、Document、UndoableEdit)的通知顺序,是否存在 Java 语言保证?

最佳答案

我不能 100% 确定您的问题是什么,但听起来您确实想使用 DocumentFilter,而不是 DocumentListener。在将文档提交到 JTextField View 之前,过滤器会获取对文档的更改,因此可以对输入进行适当的过滤。不过,我 100% 确定您不想想要使用 KeyListener。

<小时/>

编辑
您在评论中指出:

I have need of separating the two behaviors (filtering/notifying user and transmission) as two separate listeners.

但是,为什么呢?为什么有两个听众?您是否尝试使用两个 DocumentFilter?

Since I need the update to happen after a filter/validation I can't use the same kinds of listeners.

“更新”是指传输吗?或者您的意思是更新 JTextField 中显示的文本?

我仍然不确定为什么文档过滤器不适合您。如上所述,它允许您在 JTextField 中显示输入之前对其进行验证。这难道不能解决几乎所有的“顺序”问题吗?

<小时/>

编辑2

I can't prevent text input because I am using REGEXs for filtering. So, it's possible that a string of text is invalid, but could become valid, so I don't want to disallow the character since there's no way with the REGEX system in Java to determine if additional characters could validate the string. So I have to permit it, but 'validate' it (give it an OK or NOT OK).

好的,明白了。所以您不想不允许无效输入,但只是在其有效之前不传输它,对吗?

Because of some infrastructure design (fault it or not, I can't change it) I cannot combine the two behaviors of validation and transmission (which is out through a standard socket connection to another machine)

这就是我困惑的地方。您能更详细地描述这个限制吗?您究竟如何以无法从监听器内部执行此操作的方式传输它?这对于这个问题至关重要(至少就我而言)。

To finish from prior comment, the transmission needs to be only when the content is valid, so the validation has to happen before the potential transmission

所以我猜测您当前的伪代码程序工作流程是这样的:

Inside of listener
   get latest text 
   Check if text is valid or not
   if it is valid, then transmit text 
   notify user that valid text has been sent
   ? reset text field
end of listener

这是正确的吗?再说一遍,对我来说最重要的是上述限制。

<小时/>

编辑3
您声明:

Is there a Java-language guarantee as to the order in which different classes of Listeners (such as: Key, Document, UndoableEdit) on a JTextField are notified?

正如我在评论中所说,我们可以毫不犹豫地说 DocumentFilter 肯定在 DocumentListener 之前起作用。至于同一监听器的多个——正如您所知,这是未定义的。至于其他多个监听器之间的顺序,可能需要查看源码才能知道。再次,请让我们了解有关您的限制的详细信息,这是您绝对需要使用多个监听器的原因。

关于java - JTextField 监听器通知顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21439652/

相关文章:

Java http(s) 客户端没有失败

java - 使用按键绑定(bind)获取键码

java - 如何获取 TestNG 监听器所作用的类的名称?

c - 如何检查向上和向下按钮是否在 native C 中被单击

java - 了解 swt SelectionListener 中的 widgetSelected 事件和 widgetDefaultSelected 事件吗?

java - 如何用 Java 查询 SOAP 服务

java - java中如何从文件中查找字符串?

java - 我如何使用 math.random 但仅适用于某些值?

java - 为什么我的 JTable 在 OS X 上使用 VoiceOver 时总是报告为空?

java - 带按钮的 Swing JPanel 无法重绘