I'm working on a Sudoku game project. I have javafx buttons representing 81 boxes, stored in boardButtons list and userBoard which is an object of my other class (doesn't matter now I think). Method getField() returns object field which stores value of this sudoku field. I want to use bidirectional binding (it needs to be like that), so that when I select a box to and input some value, it not only shows in this box, but is also saved in userBoard's Field and vice versa.
When I run the app, after entering some values to the boxes, they stop being saved to userBoard's field. Sometimes it happens right at the beginning and sometimes I manage to enter a few numbers. I've searched for solution online but I have no idea why that happens.
StringConverter converter = new IntegerStringConverter();
boardButtons.forEach((button) -> {
try {
index = boardButtons.indexOf(button);
IntegerProperty fieldValueProperty = new JavaBeanIntegerPropertyBuilder()
.bean(userBoard.getField(index))
.name("fieldValue").build();
button.textProperty().bindBidirectional(fieldValueProperty, converter);
fieldValueProperty.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
if (gameStarted) {
System.out.println("fieldValueProperty changed: " + fieldValueProperty);
}
}
});
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
}
});