Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Bind Fields/Vars together?

 
Ranch Hand
Posts: 36
  • 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: 69403
276
  • 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.
 
Bartender
Posts: 3956
155
  • 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: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 218
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: 7108
184
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
Bartender
Posts: 3956
155
  • 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: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Observer Pattern  example

en.verejava.com/?id=20065053707352
 
Campbell Ritchie
Marshal
Posts: 69403
276
  • 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: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can create Observer class by yourself.
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic