I create several components dynamically:
for (int i = 0; i <= 2; i++) { Switch sw = new Switch(); StringProperty str = new SimpleStringProperty(String.valueOf(shop.bag[i].get())); Text text1 = new Text(); text1.textProperty().bind(str); final int finalI = i; sw.action = () - > { ind[0] = finalI * 3 + sw.index; str[ind[0]].set(String.valueOf(shop.bag[finalI * 3 + sw.index].get())); }; } The code above works fine, but initially the text is not updated, the update occurs only after calling the sw.setOnMouseClicked() method. If the StringProperty str taken out of the loop and made as a field, then all components will have the same text. How can this problem be solved?