0

I am working on a school assignment in Java using JavaFX 15. It's a simple number guessing game, where a random number is generated and the player guesses the number. When the right number is guessed the player can begin a new round or end the game. When the game ends a window displaying some statistics is supposed to appear, showing the round number, the generated number, how many guesses it took, and how long it took. I store all these stats in Array Lists as the game plays out. I would like to display this information in a TableView-

  • a column for the round number
  • a column for the generated number
  • a column for the number of guesses it took
  • a column for the time it took

but when the program is run no information appears in the table.

I created a StatRow class that can be used to add the stats to an ObservableList.

public class StatRow 
{
   private SimpleIntegerProperty number, guesses, round, time;
   
   public StatRow (int round, int number, int guesses, int time)
   {
      this.number = new SimpleIntegerProperty(number);
      this.guesses = new SimpleIntegerProperty(guesses);
      this.round = new SimpleIntegerProperty(round);
      this.time = new SimpleIntegerProperty(time);
   }
    //set and get methods here
}

Here is the controller handling the table creation/population. The formatting is handled in an fxml file. The stage is handled in the main class.

public class EndController 
{
   @FXML
   TableView<StatRow> statsReadoutTable;
   @FXML
   TableColumn<StatRow, Integer> roundCol;
   @FXML
   TableColumn<StatRow, Integer> numberCol;
   @FXML
   TableColumn<StatRow, Integer> guessesCol;
   @FXML
   TableColumn<StatRow, Integer> timeCol;
   
   ArrayList<Integer> correctNumber = new ArrayList<>();
   ArrayList<Integer> guessesNumber = new ArrayList<>();
   ArrayList<Integer> roundCount = new ArrayList<>();
   ArrayList<Integer> roundTime = new ArrayList<>();
   
   //initalizes controller with stats arrays
   public void initEndData(ArrayList correct, ArrayList guesses, ArrayList round, ArrayList start)
   { 
      correctNumber = correct;
      guessesNumber = guesses;
      roundCount = round;
      roundTime = start;
   }
   
   //constructor sets table view content
   public EndController()
   {
      numberCol = new TableColumn<>();
      numberCol.setCellValueFactory(new PropertyValueFactory<>("number"));
      
      guessesCol = new TableColumn<>();
      guessesCol.setCellValueFactory(new PropertyValueFactory<>("guesses"));
      
      roundCol = new TableColumn<>();
      roundCol.setCellValueFactory(new PropertyValueFactory<>("round"));
      
      timeCol = new TableColumn<>();
      timeCol.setCellValueFactory(new PropertyValueFactory<>("time"));
      
      statsReadoutTable = new TableView<>();
      statsReadoutTable.setItems(getStats());
      statsReadoutTable.getColumns().addAll(roundCol, numberCol, guessesCol, timeCol);
   }
   
   //create stats observable list from arrays
   private ObservableList<StatRow> getStats()
   {
      ObservableList<StatRow> stats = FXCollections.observableArrayList();
      int i;
      for(i = 0; i < roundCount.size(); i++)
      {
          stats.add(new StatRow(roundCount.get(i),correctNumber.get(i), 
                  guessesNumber.get(i), roundTime.get(i)));   
      }
      return stats;
   }
}

I have tried creating the Observable List manually with predetermined data, just to see if my getStats() method was to blame, but it still came up blank.

  • you must not create fields that are injected - you'll end up with different instances .. – kleopatra Feb 14 '21 at 15:05
  • Also see https://stackoverflow.com/questions/34785417/javafx-fxml-controller-constructor-vs-initialize-method – James_D Feb 14 '21 at 16:58
  • @James_D thought I added that one as duplicate (it's a better fit than my first) .. for some reason it was not taken, trying again – kleopatra Feb 14 '21 at 17:15

0 Answers0