#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    Melbourne, Australia
    Posts
    13
    Rep Power
    0

    Question JavaFX Multiple Binding


    In my current project, I have a requirement where the value of one Spinner needs to be able to override the value of 3-4 other Spinners.

    To expand, it's a character builder for the tabletop RPG Shadowrun. This is for the skills portion.
    There are a list of skills which have a Spinner each and there is a maximum for the overall total of them all (I've already got code for handling that).
    The problem is, there are also skill groups that contain 3-4 skills and the user needs to be able to set a value in the groups which sets the values for those skills.
    There is also a separate total for groups as skills, so any skills that are set from the skill groups need to be disregarded by the code that limits the total value of all skills.

    Pseudo-Java code might be like this:
    Code:
    ObjectProperty<Integer> propSkill[3];
    ObjectProperty<Integer> propGroup;
    
    Spinner<Integer> spnSkill[3];
    Spinner<Integer> spnGroup;
    
    spnSkill[i].bindBidirectional(propSkill[i]);
    spnGroup.bindBidirectional(propGroup);
    
    propGroup.disabledProperty().bind({propSkill[all] > 0);
    propSkill[all].disabledProperty().bind({propGroup > 0);
    
    propGroup_changed(){
    	if(propGroup > 0)
    		propSkill[all].bind(propGroup)
    	else
    		propSkill[all].unbind(propGroup)
    }
    However, the description for ObjectProperty<T>.bindBidirectional states:
    Code:
    ..it is possible to add unidirectional binding to a property with bidirectional binding and vice-versa.
    However, this practice is discouraged.
    Is my approach fine, or will it encounter issues (race conditions perhaps)? Is there a better way of doing this?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    Melbourne, Australia
    Posts
    13
    Rep Power
    0
    Should also note that the limiting code would be based on [I]propSkill.isBound()
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    Melbourne, Australia
    Posts
    13
    Rep Power
    0
    So, since I haven't received any responses, I thought I'd report how achieved the required outcome.

    Every spinner in the form has an associated ObjectProperty<Integer>. The helper class that contains the Spinner and its associated Label, value factory and property contains two methods: one to cause the spinner's value to be bound to its own ObjectProperty and one to bind the spinner's value to another spinner's ObjectProperty. A ChangeListener<Integer> calls one of the binding functions and disables the zeroed spinners.

    In the end, it was simplest to rebind the Spinner value properties and use a listener rather than rely on a chain of bindings.

IMN logo majestic logo threadwatch logo seochat tools logo