• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How ChangeListener receives its Old Value and New Value

 
V K Gupta
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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.

 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • 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?
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Mark post as helpful
  • send pies
  • 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.
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic