#Issue with bidirectional binding

1 messages · Page 1 of 1 (latest)

gusty yoke
#

Hi. I'm having an issue where my bidirectional bind is working only one way. When I change Label.styleProperty(), the data model gets updated. When updateState() is called within the model though, nothing happens, the converter isn't called. ```java
private void renderCell(final GameOfLifeCell modelCell, final PlanarCoordinates cellCoordinates) throws NoSuchMethodException {
JavaBeanBooleanProperty aliveProperty = buildAliveProperty(modelCell);
CellStateStyleConverter converter = new CellStateStyleConverter();

    Label viewCell = new Label("");
    viewCell.setMinSize(20, 20);
    viewCell.setOnMouseClicked(mouseEvent -> {
        handleCellClick(mouseEvent, cellCoordinates);
        System.out.println(aliveProperty.getValue());
    });
    viewCell.styleProperty().bindBidirectional(aliveProperty, converter);
    boardGrid.add(viewCell, cellCoordinates.width(), cellCoordinates.height());
}

private JavaBeanBooleanProperty buildAliveProperty(GameOfLifeCell modelCell) throws NoSuchMethodException {
    JavaBeanBooleanPropertyBuilder bpb = JavaBeanBooleanPropertyBuilder.create();
    return bpb
            .bean(modelCell)
            .name("value")
            .getter("isAlive")
            .setter("updateState")
            .build();
}```
cold graniteBOT
# gusty yoke Hi. I'm having an issue where my bidirectional bind is working only one way. Whe...

Detected code, here are some useful tools:

Formatted code
private void renderCell(final GameOfLifeCell modelCell, final PlanarCoordinates cellCoordinates) throws NoSuchMethodException {
  JavaBeanBooleanProperty aliveProperty = buildAliveProperty(modelCell);
  CellStateStyleConverter converter = new CellStateStyleConverter();
  Label viewCell = new Label("");
  viewCell.setMinSize(20, 20);
  viewCell.setOnMouseClicked(mouseEvent -> {
    handleCellClick(mouseEvent, cellCoordinates);
    System.out.println(aliveProperty.getValue());
  }
  );
  viewCell.styleProperty().bindBidirectional(aliveProperty, converter);
  boardGrid.add(viewCell, cellCoordinates.width(), cellCoordinates.height());
}
private JavaBeanBooleanProperty buildAliveProperty(GameOfLifeCell modelCell) throws NoSuchMethodException {
  JavaBeanBooleanPropertyBuilder bpb = JavaBeanBooleanPropertyBuilder.create();
  return bpb.bean(modelCell).name("value").getter("isAlive").setter("updateState").build();
}
#

<@&987246487241105418> please have a look, thanks.

gusty yoke
#

I checked one more thing, and it seems like JavaBeanProperty gets updated properly, but styleProperty doesnt

#

Although my deduction here might be wrong

gusty yoke
#

I debugged it more thorougly and it seems aliveProperty doesn't update correctly after all

#
      aliveProperty.addListener((observable, oldValue, newValue) -> {
            System.out.println("Property changed: " + oldValue + " -> " + newValue);
        });``` i used this to check if the property changes when model is updated
#

and it doesnt

simple niche
#

I never had much luck with bidirectional binding tbh either. I only use binding sparingly and unidirectionally if I do. Once properties are bound, they have to be unbound if something outside wants to change them. The stuff I bind I don't intend for anyone else to change. The way I've seen "bidirectional binding" done is by using listeners to update each other. Not the cleanest way ever, but it does work.

#

I wish I had a better answer for ya, and I feel for you. If you find a solution lmk!

cold graniteBOT
#

@gusty yoke

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

gusty yoke