• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Bind Fields/Vars together?  RSS feed

 
Ranch Hand
Posts: 34
  • 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: 64171
215
  • 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.
 
Master Rancher
Posts: 3189
119
  • 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: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 155
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: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • 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
Master Rancher
Posts: 3189
119
  • 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: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Observer Pattern  example

en.verejava.com/?id=20065053707352
 
Campbell Ritchie
Marshal
Posts: 64171
215
  • 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: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can create Observer class by yourself.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!