1

I want to change different instance variables in every loop of a for-loop. I can't do it with an array or ArrayListbecause the variables are @FXML definitions.

See the example below:

    @FXML
    public label foo1;
    @FXML
    public label foo2;

    public void changeLabels() {
      for (int i = 0; i < 2; i++) {

         //Here I want to access the variables foo1/foo2 depending on the value of i    

         // Thought about something like
         //      String temp = "foo" + (i + 1);
         // but I dont know how to go on
      }
    }

Why can't I ask this question? It was marked as duplicate but I explained why the linked question wont work for me.

glglgl
  • 89,107
  • 13
  • 149
  • 217
MarcKamm
  • 27
  • 7
  • There are two ways do do it: either you use a simple `if` statement, or you use reflection and obtain a `Field` from the class with a dynamically created name. A third way would involve creating a self-crafted annotation, but that would probably be overkill. Once the question has been reopened, I can elaborate on how to do it. – glglgl Feb 09 '18 at 14:33

0 Answers0