0

I have to set values for labels, I am set values by following way which is very lengthy,

public class FXMLDocumentController implements Initializable {

    private TilesLogic gl = new TilesLogic();
    private List<List<Integer>> game;

    @FXML
    private Label C1;
    @FXML
    private Label C2;
    @FXML
    private Label C3;
    @FXML
    .
    .
    .
    private Label C16;
    @FXML
    private Label timer;


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        game = gl.shuffle();
        SetValues();
    }    

    void SetValues(){
       C1.setText(String.valueOf(game.get(0).get(0)));
       C2.setText(String.valueOf(game.get(0).get(1)));
       .
       .
       C15.setText(String.valueOf(game.get(3).get(2)));
       C16.setText(String.valueOf(game.get(3).get(3)));
    }
}

is that any other way just like that

    for(int i =1;i<17;i++)
           setText("#C"+String.valueOf(i),String.valueOf(game.get(i))); 
           // setText("#id","Sometext");

to do it simple way and less time ?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Shiva_Adasule
  • 769
  • 2
  • 9
  • 14
  • 5
    no that's not possible - but you are not _forced_ to use fxml :) simply don't if pure code is easier to setup. unrelated: java naming conventions please! – kleopatra Mar 18 '20 at 15:23
  • 4
    You could structure your view in such a way that a `Parent` nodes holds all of the `Labels` only. Then you can do something like `parent.getChildren().foreach(node-> ((Label)node).setText("..."));` – SedJ601 Mar 18 '20 at 16:22
  • 3
    You can put the elements into a list via fxml file, see https://stackoverflow.com/a/34470630/2991525 However ask yourself, if creating&adding these labels could be done inside the `initialize` method using a loop; if this is possible with a reasonable amount of code, it's usually preferable to creating multiple elements via fxml with similar structure (Don't repeat yourself!). – fabian Mar 18 '20 at 17:30

0 Answers0