Avoiding unnecessary events and infinite loop in “coupled” JSliders?

前端 未结 4 1374
余生分开走
余生分开走 2020-12-21 05:50

I have a GUI that is issuing commands to a web server based on slider values. Some of these sliders are \"coupled\" on the web server, so changing one of them may also chang

相关标签:
4条回答
  • 2020-12-21 06:20

    One author calls this problem fibrillation. This discussion suggests a shared model, in addition to the flag approach suggested by @Pyrolistical.

    0 讨论(0)
  • 2020-12-21 06:21

    The standard model used in .Net WPF is to fire an event only if value of the property has changed! In your case it is setValue() method.

    0 讨论(0)
  • 2020-12-21 06:22

    After struggling with this for a while and going into a mess with various booleans, I found that the solution was to simply add a check for hasFocus and respond accordingly.

    • If a field did NOT have focus, then it would allow an update to itself, but wouldn't trigger updates to any other field.

    • If a field DID have focus, then it would trigger changes to other fields.

    This way, only the focused field can trigger any change and there is no infinite loop. The code is readable and simple, now, and the logic is easy to follow and update.

    0 讨论(0)
  • 2020-12-21 06:34

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

    I wouldn't add and remove listeners as that just triggers more listeners!

    0 讨论(0)
提交回复
热议问题