简体   繁体   English

JTextField侦听器通知顺序

[英]JTextField Listener Notification Order

I am using a JTextField, wherein I am filtering input coming in via REGEXs and notifying the user (background color changes) and then pushing the updates out over a socket (character by character, not a 'Hit enter when you're done' kind of behavior, by spec) 我使用的是JTextField,其中我过滤通过REGEX输入的输入并通知用户(背景颜色变化),然后将更新通过套接字推送出去(按字符逐个显示,而不是“完成后点击进入”)行为,按规范)

The issue is, since there's no guaranteed order of DocumentListener notification, I cannot put both the transmission of the update and the validation of the update on the same kind of listener. 问题是,由于没有确保DocumentListener通知的顺序,因此我无法将更新的传输和更新的验证都放在同一种侦听器上。 Is there any guaranteed order of notification amongst different kinds of Listeners (KeyListener vs DocumentListener vs ... ) with a JTextField? 在使用JTextField的不同类型的侦听器(KeyListener与DocumentListener与...)之间,是否有保证的通知顺序?

I can find no helpful documentation about between different kinds of Listeners, only (it seems) within a single kind of Listener (such as a DocumentListener) 我找不到关于不同种类的侦听器之间的有用文档,仅(似乎)在一种侦听器(例如DocumentListener)中

Thanks! 谢谢!

EDIT 1 Validation - REGEX validation does not permit me to disallow any text coming in since additional characters may make an 'invalid' string 'valid'. 编辑1验证-REGEX验证不允许我禁止输入任何文本,因为其他字符可能会使“无效”字符串“有效”。

Separation of Behavior - By design, I cannot merge the two behaviors into one listener, they are being set up at different times in the code flow and that cannot be changed. 行为分离-根据设计,我无法将这两种行为合并为一个侦听器,它们是在代码流中的不同时间设置的,因此无法更改。 It's a protocol design issue that's not up for debate. 这是一个协议设计问题,不容争论。 I can't really go into more useful detail without just explaining the whole thing. 我不能仅仅解释整个事情就不能真正地进行更有用的详细介绍。 The salient point is they are just separated into two listeners. 突出的一点是它们只是分为两个侦听器。 I need to make the transmission step happen after the validation step, so I need to know if there is a CONCRETE definition of Listener KIND notification. 我需要使传输步骤发生在验证步骤之后,因此我需要知道是否存在侦听器KIND通知的CONCRETE定义。 By Kind I mean KeyListener vs DocumentListener vs UndoableEditListener. 我的意思是KeyListener vs DocumentListener vs UndoableEditListener。 I know within each kind of listener no order is guaranteed. 我知道每种听众都无法保证顺序。

However, is it guaranteed that DocumentListeners are all notified before UndoableEditListeners or vice versa? 但是,是否可以确保在UndoableEditListeners之前通知所有DocumentListeners,反之亦然? Or are all different kinds of listeners just notified in no particular order whatsoever. 还是所有不同种类的侦听器都没有特定的顺序通知。

EDIT 2 编辑2

Sorry, it seems as we are getting lost in the weeds hardcore. 抱歉,似乎我们迷失了杂草的核心。

At this moment, all I am trying to figure out is: 目前,我要弄清楚的是:

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? 是否有Java语言保证关于JTextField上不同类别的侦听器(例如:Key,Document,UndoableEdit)的通知顺序?

I'm not 100% sure of exactly what your problem is, but it sounds like you really want to use a DocumentFilter, not a DocumentListener. 我不是100%确切地确定您的问题是什么,但这听起来像您确实要使用DocumentFilter而不是DocumentListener。 The filter gets changes to the document before they have been submitted to the JTextField view, and so allows for decent filtering of input. 过滤器在将更改提交到JTextField视图之前获取对文档的更改,因此可以对输入进行适当的过滤。 I am 100% sure however that you don't want to use a KeyListener. 我100%肯定然而,你希望使用的KeyListener。


Edit 编辑
You state in comment: 您发表评论:

I have need of separating the two behaviors (filtering/notifying user and transmission) as two separate listeners. 我需要将两个行为(过滤/通知用户和传输)分离为两个单独的侦听器。

But again, why? 但是又为什么呢? Why two listeners? 为什么要有两个听众? Are you trying to use two DocumentFilters? 您是否要使用两个DocumentFilters?

Since I need the update to happen after a filter/validation I can't use the same kinds of listeners. 由于我需要在过滤器/验证之后进行更新,因此无法使用相同种类的侦听器。

By "update" do you mean transmission? “更新”是指传输吗? Or do you mean updating the text displayed in the JTextField? 还是说更新JTextField中显示的文本?

I am still not sure why a Document Filter won't work for you. 我仍然不确定为什么文档过滤器对您不起作用。 As noted above, it allows you to validate input before it gets displayed in the JTextField. 如上所述,它允许您在输入显示在JTextField中之前对其进行验证。 Wouldn't this solve pretty much any "order" problems? 这样难道不能解决任何“订单”问题吗?


Edit 2 编辑2

I can't prevent text input because I am using REGEXs for filtering. 我无法阻止文本输入,因为我正在使用REGEX进行过滤。 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. 因此,一串文本可能无效,但可能变为有效,因此我不想禁止使用该字符,因为Java中的REGEX系统无法确定其他字符是否可以验证该字符串。 So I have to permit it, but 'validate' it (give it an OK or NOT OK). 因此,我必须允许它,但要“验证”它(给它“确定”或“不确定”)。

OK, understood. 好的了解了。 So you do not want to not allow invalid input, but just not transmit it until it's valid, correct? 因此,您不想不允许无效的输入,而只是在有效之前才传输它,对吗?

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) 由于某些基础结构设计(有无故障,我无法更改),因此无法组合验证和传输这两种行为(通过与另一台计算机的标准套接字连接来完成)

This is where I'm confused. 这就是我感到困惑的地方。 Can you describe this limitation in greater detail? 您能否更详细地描述此限制? How exactly are you transmitting it in such a way that you can't do so from within the listener. 您如何精确地传输它,以致于无法从侦听器内部进行传输。 This is of critical importance to this question (at least as far as I am concerned). 这对这个问题至关重要(至少就我而言)。

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 为了从先前的评论结束,传输仅需在内容有效时进行,因此验证必须在潜在传输之前进行

So I'm guessing that your current program work-flow in pseudocode goes something like this: 因此,我猜测您当前的伪代码程序工作流程如下:

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

Is this correct? 这个对吗? And again, of paramount importance to me is the restriction described above. 同样,对我来说最重要的是上述限制。


Edit 3 编辑3
You state: 您声明:

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? 是否有Java语言保证关于JTextField上不同类别的侦听器(例如:Key,Document,UndoableEdit)的通知顺序?

Again as I've stated in comment, one can state without hesitation that a DocumentFilter definitely acts before a DocumentListener acts. 再次如我在评论中所述,可以毫不犹豫地声明DocumentFilter肯定在DocumentListener起作用之前起作用。 As for multiples of the same listener -- that as you know is undefined. 至于同一侦听器的倍数-您所知道的是未定义的。 As for the order between multiple other listeners, you may need to look at the source code to find out. 至于其他多个侦听器之间的顺序,您可能需要查看源代码才能找到。 Again, please let us know the details about your restriction, the source of your absolute need to use more than one listener. 同样,请让我们知道有关您的限制的详细信息,您绝对需要使用多个监听器。

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

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