Keep in mind that all event listeners are executed in the Swing event stream. So things may not go exactly as you want. In this situation, any decision will be hack-ish, because you cannot get full control over the swing stream and who posts events on it.
I am trying to say the following: suppose you decide to use some flag so that your listeners know that these are programmatic changes. Here is a possible scenario (I assume that you are following the correct rule to do any UI updates from the swing stream via invokeLater ):
- set flag to skip events
- Settext
- set flag to false
if you do all this in one call, setText will trigger update events sent to the end of the event queue, so the flag will be false by the time they are executed.
So you have to do steps 1 and 2 in one invokeLater call or even invokeAndWait , and then send another event using invokeLater to disable the flag. And pray that your user doesnβt make some changes between these two calls so quickly, otherwise they will also be considered as program changes.
Denis tulskiy
source share