1

Is there a preferred way to prevent SpinnerNumberModel from firing ChangeEvents?

I could have sworn that I could call something like .setIsAdjusting(false) somewhere and this would disable the event. But it doesn't seem so.

Or do I just need to extend SpinnerNumberModel?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Lucas
  • 1,869
  • 4
  • 20
  • 36
  • 1
    And the obvious question ... why ? – Robin Jun 05 '13 at 08:20
  • _Is there a preferred way to prevent SpinnerNumberModel from firing ChangeEvents?_ No and you should not need to do that. Except breaking the corresponding `JSpinner` what would you achieve with that? I second Robin's question, why would you do that? – Guillaume Polet Jun 05 '13 at 08:25
  • @Robin Perhaps I'm just going about it in a completely silly way, but I have a couple of spinners and the values that they have are interdependent. Say, for the sake of argument, the value of one is the square of the other. – Lucas Jun 05 '13 at 08:25
  • I do not see how that would require to disable events. I would say you just need the event to update the second spinner when the first spinner's value is changed – Robin Jun 05 '13 at 08:34
  • @Robin The problem then is that changing the second spinners value fires off another event which changes the... aha, that's why the events only get fired when the value is different from the current value. – Lucas Jun 05 '13 at 08:46
  • @Lucas I see 3 options here: 1) Your synching between the 2 spinners is stable (setting a value in spinner 1 sets a value in spinner 2 which will try to set the same value in spinner 1) so eventually ChangeEvent's will not fire infintely. 2) Whenever you want to change a value in a spinner you disable the listener of that spinner by setting a flag (`if (updating) return; updating = true; ...`) 3) Whenever you want to change a value in a spinner, you remove you listener first and add it back afterwards. – Guillaume Polet Jun 05 '13 at 08:51
  • @Guillaume Polet this quite not possible with (Property)ChangeListener, because BasicSpinnerUI has ...., and firing forever, I will post an SSCCC, please to update if you are know how ... – mKorbel Jun 05 '13 at 08:55
  • @mKorbel From the code of `SpinnerNumberModel`: `if (!value.equals(this.value)) { this.value = (Number)value; fireStateChanged(); }`. So if the value is `equals`, it won't fire an additional event. – Guillaume Polet Jun 05 '13 at 08:58
  • @Guillaume Polet hmmm agreed or not, my last brain cell died, I'm quite lost, this can be misspelled by constructor or used methods, I have shadowing idea (really haven't any), do you have some post about that – mKorbel Jun 05 '13 at 09:01
  • 1
    @GuillaumePolet 1) when rounding errors are stable there wont be an infinite loop for two components. However, I have more than 2 and unless I lock the components a cycle is easy to initiate. 2) Will try that. 3) Yuck – Lucas Jun 05 '13 at 09:10
  • I can do that with BoundedRangeModel, but with plain SpinnerXxxModel I'm out of luck, this's reason why I saw (a few times) to remove all Action/Mouse/(Property)ChangeListeners for Editors JComponents, then to add own XxxListener back – mKorbel Jun 05 '13 at 09:58
  • got it see (code source) how [JCalendar by Kai Toedter](http://www.toedter.com/en/jcalendar/) has implemented JSpinner, override SpinnerXxxModel, and resticted ChangeListener with two separate flags – mKorbel Jun 05 '13 at 10:08
  • @GuillaumePolet thanks, but then to use [BoundedRangeModel](http://stackoverflow.com/questions/7617605/java-jslider-precision-problems), really excelent for JScrollBar(s) – mKorbel Jun 05 '13 at 10:19

0 Answers0