简体   繁体   中英

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)

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. Is there any guaranteed order of notification amongst different kinds of Listeners (KeyListener vs DocumentListener vs ... ) with a JTextField?

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)

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'.

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. By Kind I mean 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? Or are all different kinds of listeners just notified in no particular order whatsoever.

EDIT 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?

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. The filter gets changes to the document before they have been submitted to the JTextField view, and so allows for decent filtering of input. I am 100% sure however that you don't want to use a 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?

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?

I am still not sure why a Document won't work for you. 对您不起作用。 As noted above, it allows you to validate input before it gets displayed in the JTextField. Wouldn't this solve pretty much any "order" problems?


Edit 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).

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
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?

Again as I've stated in comment, one can state without hesitation that a DocumentFilter definitely acts before a DocumentListener acts. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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