• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interdepenent JSpinners problem  RSS feed

 
Veronique Kus
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have two JSpinner which represent the width and the height of an image. These values can be manipulated freely unless "Maintain ratio" checkbox is selected. Then, when e.g. width changes, the height value is changed accordingly automatically using the previously calculated size ratio for the image.

This little program represents the idea (I hard-coded the size ratio):



I attached changes listeners to both JSpinners so that when one value is changes, the value of the other JSpinner can be changed in response. The problem is that the stateChanged function is called also as a result of this automatic ratio adjustment, so that everything gets messed up. I was wondering how I could make this properly... Is there a way of firing the stateChanged event only when the value is changed by user input, not when it is changes programatically by ratio adjustments?

I would really appreciate some help. Thank you!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
easiest way is to remove the listener, then add it back, which requires a few mods to your program
(and you could do it with a single listener, but easier to maintain if separated)

 
Veronique Kus
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, that was quite simple, should've worked it out myself... Anyways, works beautifully now, thank you very much!
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to solve the problem is with a custom SpinnerNumberModel.

But while this was a fun exercise in Swing model manipulation, I think that Michael's way is simpler and thus less likely to result in errors. Though on the other hand, if you need to tie several JSpinner models together, then perhaps I would prefer a variation on my technique (I would have to notify multiple models of changes via an observer/listener pattern).

 
Veronique Kus
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for an alternative solution, it might come in useful in the future!
 
Rob Spoor
Sheriff
Posts: 21052
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of removing and re-adding listeners you can also use a boolean; initially false, you set it to true when you call a method that would trigger an event. The event listener simply does nothing if the boolean is true. In code:
 
Veronique Kus
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for another solution. Is any of them preferred in terms of performance etc.?
 
Rob Spoor
Sheriff
Posts: 21052
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that's micro-optimization. Whichever you like best is best
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:Another way to solve the problem is with a custom SpinnerNumberModel.

I like your approach. My gut feeling is that data constraints belong in the model.

I had done something a bit similar in
http://forums.sun.com/thread.jspa?threadID=5439906
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!