Avoid unnecessary events and an endless loop in "connected" JSliders?

I have a graphical interface that issues commands to the web server based on the values ​​of the slider. Some of these sliders are “connected” to the web server, so changing one of them can also change the other. Communication is performed by the web server, returning a list of values ​​that were set based on the issued command.

Thus, I can easily install the appropriate sliders based on this answer, but the problem is that it causes ChangeListener to start and then be issued to the web server again. Ideally, the “clutch” should behave well and avoid endless loops, but this is a potential problem, and sending all these extra events seems unnecessary.

Two solutions I could think of were:

  • Temporarily remove listeners, change the value, and then return them.
  • Add a “manual” flag to let the listener know that he should ignore the change.

None of them seem like the perfect solution for me, but is one of them “better” than the other? Or is there a third solution that I am not considering?

+4
source share
3 answers

add enabled flag to listeners and disable them before manually setting value

I would not add or remove listeners, as this caused more listeners!

+3
source

One author calls this fibrillation problem. This discussion offers a general model, in addition to the flag approach proposed by @Pyrolistical.

+3
source

The standard model used in .Net WPF is designed to trigger an event only when the property value changes! In your case, this is the setValue() method.

0
source

Source: https://habr.com/ru/post/1306855/


All Articles