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

前端 未结 4 1392
余生分开走
余生分开走 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: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.

提交回复
热议问题