I make the game like a monopoly, and a question has arisen over which I am already sitting for a couple of days. In general, when creating a new game (pressing the "New Game" button), the menu pops up, in which the player must choose how many players should be in his game and, if desired, can give them names. The class of the new game in turn must return a collection of player names. The problem is that in the main program, when I press the button, immediately after the window opens, the value is transferred to zero and goes further, where I have a check for zero.
arrayLengthCounter = arr.size(); System.out.println(arrayLengthCounter); if(arr != null) { for (int i = 0; i < arrayLengthCounter; i++) { playerHBox = addPlayerHBox(); System.out.println("hbox created"); } } After that, it passes the test, and after that when I set the value in the window, it returns to the variable in the maine, but it no longer passes through the cycle and creates nothing more. I had the second option so that the class of the new game would return immediately ready-made hboxes in the specified quantity, but the problem remained the same, the values were simply not added to the layout.
if(hueHueBox != null){ layout.getChildren().addAll(hueHueBox); } And it would be okay if only in the new game there was such a problem, but it is the same when buying any buildings, because there is a similar execution mechanism.
buyPanelHouse.setOnAction(e -> { int n = buyPanelHouseMenu.display(); System.out.println(n); if(n == 1){ Rectangle r = new Rectangle(120,30); //должен просто создаться прямоугольник в окне r.setFill(Color.RED); GridPane.setConstraints(r, 0,0); hBoxGridPaneProcess.getChildren().addAll(r); } }); I would appreciate any help and / or advice
Window creation class:
import javafx.scene.text.Text; import javafx.stage.*; import javafx.scene.*; import javafx.scene.layout.*; import javafx.scene.control.*; import javafx.geometry.*; import java.util.ArrayList; import static java.lang.Double.MAX_VALUE; public class createMenuBox { private static int n; private static HBox hueHueBox = null; public static HBox display() { Stage window = new Stage(); VBox layout = new VBox(10); layout.setPadding(new Insets(20, 20, 20, 20)); TextField[] tfArr = new TextField[5]; ArrayList<String> returnArr = new ArrayList<>(); window.initModality(Modality.APPLICATION_MODAL); window.setTitle("Подготовка"); ChoiceBox<Integer> choiceBox = new ChoiceBox<>(); Scene scene = new Scene(layout,400, 500); window.setScene(scene); window.show(); //default option choiceBox.getItems().addAll(2,3,4,5); choiceBox.setValue(2); choiceBox.setMaxSize(MAX_VALUE, MAX_VALUE); Text windowTitle = new Text("Задайте колличество игроков"); Text windowSecondTitle = new Text("Отличный выбор!"); Text playerNameTitle = new Text("Нажмите, если вам не нравятся стандартные имена"); Text startButtonTitle = new Text("Нажмите, если считаете себя готовым"); Text startButtonSecondTitle = new Text("Пожалуйста, не оставляйте поля пустыми"); layout.getChildren().addAll(windowTitle); Button btn = new Button("Сохранить"); layout.getChildren().addAll(choiceBox, btn); Button buttonSetNames = new Button("Задать имена"); Button startBtn = new Button("Готово"); startBtn.setMaxSize(MAX_VALUE, MAX_VALUE); btn.setMaxSize(MAX_VALUE, MAX_VALUE); btn.setOnAction(e -> { getChoice(choiceBox); System.out.println(n); layout.getChildren().remove(windowTitle); layout.getChildren().add(windowSecondTitle); layout.getChildren().add(buttonSetNames); buttonSetNames.setMaxSize(MAX_VALUE, MAX_VALUE); layout.getChildren().add(playerNameTitle); layout.getChildren().add(startBtn); layout.getChildren().add(startButtonTitle); startBtn.setOnAction(ee -> { for (int i = 0; i < n; i++) { if (tfArr[i] == null){ returnArr.add(i, ("Игрок-" + (i+1))); } System.out.println(returnArr.get(i)); } if(returnArr != null) { for (int i = 0; i < returnArr.size(); i++) { hueHueBox = Main.addPlayerHBox(); System.out.println("hbox created"); } } window.close(); }); choiceBox.setDisable(true); btn.setDisable(true); }); buttonSetNames.setOnAction(e -> { layout.getChildren().remove(startButtonTitle); layout.getChildren().remove(playerNameTitle); layout.getChildren().remove(windowSecondTitle); layout.getChildren().remove(startBtn); buttonSetNames.setDisable(true); for (int i = 0; i < n; i++) { tfArr[i] = new TextField(); layout.getChildren().add(tfArr[i]); } startBtn.setMaxSize(MAX_VALUE, MAX_VALUE); layout.getChildren().add(startBtn); layout.getChildren().add(startButtonSecondTitle); startBtn.setOnAction(ee -> { for (int i = 0; i < n; i++) { returnArr.add(i, tfArr[i].getText()); if (tfArr[i].getText().trim().isEmpty()){ returnArr.add(i, ("Игрок-" + (i+1))); } System.out.println(returnArr.get(i)); } if(returnArr != null) { for (int i = 0; i < returnArr.size(); i++) { hueHueBox = Main.addPlayerHBox(); System.out.println("hbox created"); } } window.close(); }); }); return hueHueBox; } private static void getChoice(ChoiceBox<Integer> choiceBox) { n = choiceBox.getValue(); } }