• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interdepenent JSpinners problem

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm, that was quite simple, should've worked it out myself... Anyways, works beautifully now, thank you very much!
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for an alternative solution, it might come in useful in the future!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for another solution. Is any of them preferred in terms of performance etc.?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that's micro-optimization. Whichever you like best is best
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic