Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Bind Fields/Vars together?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there  - suppose I have:



is there any way to set up a binding between x and y so that if one changes, the other changes to the same value and vice-versa? This is a major simplification of the actual code/design, but if i could get this specific answer, it would be very helpful.

THANKS!
 
Marshal
Posts: 65056
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find out what the current state of the Observer Pattern is. I don't know much more than that, I am afraid.
 
Saloon Keeper
Posts: 3413
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can work with bound properties. See here for an explanation:
beans properties
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 186
5
MS IE Notepad Suse
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
although it seems easy possible - it kinda seems code smell - if you want to entangle fields - then why just don't use a single field?
try to keep the same or dependent information is quite error prone - if possible the base data should kept only once and the dependent data should be evaluated at the time they're needed (aka in getters used to retrive dependent data instead of direct field access)
 
Sheriff
Posts: 6110
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that JavaFX has a load of Observable classes, such as ObservableIntegerValue, but I don't know if they work outside of the JavaFX environment.  Also, it would tie your code to Java 8 unless you import the JavaFX base. (JavaFX is no longer packaged with Java 9 and above).
 
Piet Souris
Saloon Keeper
Posts: 3413
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a very interesting suggestion by Knute. I have no experience with JavaFX and its binding possibilities, time for me to try some things out!!

But here is a silly exapmple how you could do such a thing in Java:
 
joseph hu
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Observer Pattern  example

en.verejava.com/?id=20065053707352
 
Campbell Ritchie
Marshal
Posts: 65056
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, the classes used by that last example have been deprecated.
 
joseph hu
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can create Observer class by yourself.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!