• 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

How ChangeListener receives its Old Value and New Value

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I am trying my hands on JavaFX. While using Property and ChangeListener, I am stuck at point that "how a changeListener receives its value". I am writing a code below, kindly tell me how the values are passed to the ChangeListener.

 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An instance of SimpleStringProperty (as well as any other class in the Property hierarchy) stores its current value in a member field.
When you call set(newValue), JavaFX
> copies the current value of that field to a temporary variable

> assigns the new value to the member field

> and calls changed() of every registered listener, passing it the old value (from temporary variable) and the new value of the member field.
changed() is invoked after the value is changed.

Does that answer your question?
 
Rancher
Posts: 387
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A ChangeListener is an observer pattern. The actual implementation of this in the JavaFX code is a reasonably complex and abstract piece of code to serve as a generic system that can work efficiently for the many different types of Properties and Bindings that JavaFX supports, so studying the actual JavaFX source on this would probably just confuse you.

However, to help understand in general how observers work (even though the JavaFX implementation differs slightly), you can see a simplified code for a straightforward observer style processing at http://www.tutorialspoint.com/design_pattern/observer_pattern.htm To map those concepts back to the JavaFX setup, the Subject is the Property being observed and the ChangeListener is the observer of the changes to the property.
 
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't speak for JavaFX, but in JSF, the ChangeListener fires before the value is changed. Like so:

1) The new value comes into the JSF Controller as part of a (validated) form submission
2) The current value is queried (via the "get" accessor) by the Controller.
3) IFF the new value is not equal to the old value, the ChangeListener fires (the Controller invokes the listener method), and the incoming new value and old values are passed as parameters to the listener.
 
reply
    Bookmark Topic Watch Topic
  • New Topic