问题描述
我有一个GUI,它根据滑块值向Web服务器发出命令。其中一些滑块在Web服务器上耦合,因此更改其中一个滑块也可能会改变另一个滑块。耦合是由Web服务器返回根据发出的命令设置的值列表完成的。
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 change another one. The coupling is accomplished by the web server returning a list of the values that were set based on the the issued command.
因此我可以根据此设置相应的滑块响应,但问题是这样做会导致ChangeListener被触发,然后再次向Web服务器发出命令。理想情况下,耦合应该表现良好并避免无限循环,但这是一个潜在的问题,发送所有这些额外的事件似乎是不必要的。
So I can easily set the appropriate sliders based on this response, but the issue is that doing this causes the ChangeListener to be fired and then a command is issued to the web server again. Ideally, the "coupling" should be well behaved and avoid infinite loops, but that is a potential concern and send all those extra events seems unnecessary.
我能做的两个解决方案想到的是:
The two solutions I could think of were:
- 暂时删除监听器,更改值,然后将它们放回去。
- 添加一个手动标志,让听众知道它应该忽略更改。
这些都不是理想的我的解决方案,但其中一个比另一个更好?或者我有没有考虑第三种解决方案?
Neither of those seems like the ideal solution to me, but is one of them "better" than the other? Or is there a third solution that I'm not considering?
推荐答案
添加启用
标记到侦听器并在手动设置值之前禁用它们
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!
这篇关于避免不必要的事件和“耦合”的无限循环。 JSliders?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!