JavaFX, updating textProperty



  • Create dynamically several components:

    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 is working normally, but the original text is not updated, the update is only after the method is called. sw.setOnMouseClicked()♪ If StringProperty str Remove the cycle and make it a field, all components will have the same text. How can we solve this problem?



  • You try the code.

    ind[0] = finalI * 3 + sw.index;
    str[ind[0]].set(String.valueOf(shop.bag[finalI * 3 + sw.index].get()));
    

    Place outside sw.action♪ Either check what you've got. shop.bag[i].get() at the time of initialization.

    You have no initialization.

    Updating

    Maintain the elements created dynamically in ArrayList and then, for each element, initialize their properties after uploading the data to shop.bag. So you also have a backup index.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2