0

Im working on some card game in java. I'm using javaFX for an user inteface but i have a little problem with it. I have 25 buttons and i would like to add text to all of them. I have done it but the code is ugly and very long. I'm using scenebuilder and I created button there, every button has uniqe id from 1 to 25. Is there any way to shorten this code?

   @FXML
   Button card1;
   @FXML
   Button card2;
   @FXML
   Button card3;
   @FXML
   Button card4;
   @FXML
   Button card5;
   @FXML
   Button card6;
   @FXML
   Button card7;
   @FXML
   Button card8;
   @FXML
   Button card9;
   @FXML
   Button card10;
   @FXML
   Button card11;
   @FXML
   Button card12;
   @FXML
   Button card13;
   @FXML
   Button card14;
   @FXML
   Button card15;
   @FXML
   Button card16;
   @FXML
   Button card17;
   @FXML
   Button card18;
   @FXML
   Button card19;
   @FXML
   Button card20;
   @FXML
   Button card21;
   @FXML
   Button card22;
   @FXML
   Button card23;
   @FXML
   Button card24;
   @FXML
   Button card25;

   @FXML
   public void initialize(){
       Board board = new Board();
       board.createBoard();
       card1.setText(board.getListFields().get(0).getWord().name());
       card2.setText(board.getListFields().get(1).getWord().name());
       card3.setText(board.getListFields().get(2).getWord().name());
       card4.setText(board.getListFields().get(3).getWord().name());
       card5.setText(board.getListFields().get(4).getWord().name());
       card6.setText(board.getListFields().get(5).getWord().name());
       card7.setText(board.getListFields().get(6).getWord().name());
       card8.setText(board.getListFields().get(7).getWord().name());
       card9.setText(board.getListFields().get(8).getWord().name());
       card10.setText(board.getListFields().get(9).getWord().name());
       card11.setText(board.getListFields().get(10).getWord().name());
       card12.setText(board.getListFields().get(11).getWord().name());
       card13.setText(board.getListFields().get(12).getWord().name());
       card14.setText(board.getListFields().get(13).getWord().name());
       card15.setText(board.getListFields().get(14).getWord().name());
       card16.setText(board.getListFields().get(15).getWord().name());
       card17.setText(board.getListFields().get(16).getWord().name());
       card18.setText(board.getListFields().get(17).getWord().name());
       card19.setText(board.getListFields().get(18).getWord().name());
       card20.setText(board.getListFields().get(19).getWord().name());
       card21.setText(board.getListFields().get(20).getWord().name());
       card22.setText(board.getListFields().get(21).getWord().name());
       card23.setText(board.getListFields().get(22).getWord().name());
       card24.setText(board.getListFields().get(23).getWord().name());
       card25.setText(board.getListFields().get(24).getWord().name());

   }```
kubas1178
  • 41
  • 5
  • 5
    It looks like it would be much better to do at least this part of the UI in Java, instead of in FXML. – James_D Jan 19 '21 at 21:25

2 Answers2

2

Something like is what everyone means when they say you should create those items in java and not in fxml

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {

        GridPane gridPane = new GridPane();

        //This is so you can access them later
        ArrayList<Button> buttonList = new ArrayList<>();

        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 5; j++) {
                Button button = new Button();
                button.setText("Card:"+i*j);
                button.setPrefSize(80,120);
                //If you need to know what card it is add the below line
                button.setId(String.valueOf(i*j));

                buttonList.add(button);

                gridPane.add(button, i, j);
            }
        }

        Scene scene = new Scene(gridPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
Matt
  • 3,052
  • 1
  • 17
  • 30
0

You can create a list of buttons like follows:

List<Button> buttons = Arrays.asList(card1, card2, card3, ..., card25);

Then iterate over it:

for (int i = 0; i < buttons.size(); i++) {
    buttons.get(i).setText(board.getListFields().get(i).getWorld().name());
}
Nailuj29
  • 750
  • 1
  • 10
  • 28
  • 1
    What is `EnumeratedItem` and `ListUtils`? Why not just do `for (Button btn : buttons) { ... }`? – Slaw Jan 19 '21 at 22:46
  • 1
    you need the index of the item in the list. See this answer for more details: https://stackoverflow.com/a/34804751/12627634 – Nailuj29 Jan 19 '21 at 22:49
  • 1
    Ah, I see. Unfortunately that code seems custom made specifically for that question. It's useful but may be overkill for the OP (they'd either have to write the code or pull in an entire library). In this case it'd probably be easier to use a traditional `for` loop (i.e. an index-based for loop), assuming the list is `RandomAccess`. – Slaw Jan 19 '21 at 22:52
  • That would work too. I don't know why i didn't think of that. Edited my answer – Nailuj29 Jan 19 '21 at 22:53
  • 3
    While this is definitely an improvement on the code in the question, it’s still not scalable. (You’ve hidden the ugly part behind your ellipsis, but it’s still ugly.) Would you want this solution with 100 buttons? 1000? What if the number of buttons was specified dynamically in some way. (And I understand that’s not in the question, but it’s never good programming practice to assume something like that is fixed.) – James_D Jan 19 '21 at 23:59
  • i agree. It would be best for OP to use an imperative approach to this problem, rather than FXML – Nailuj29 Jan 20 '21 at 14:34