Reputation: 117
Hey guys I am trying to do a scorelist for a Jump&Run game for a project in university I am saving the data of a Player
class with the attributes nickname
and finalScore
in an ArrayList
. I want to show the data in a FXML TableView
but it won't work and only shows no content.
I have already tried to declare the attributes as SimpleString
/IntegerProperty
s but it did not change.
public class ScoreController implements Initializable {
@FXML
private TableView<Player> table;
@FXML
private TableColumn<Player, String> Nickname;
@FXML
private TableColumn<Player, Integer> Score;
// Creating Observable Array List
private ObservableList<Player> data = FXCollections.observableArrayList();
// Set up the Scene
private Parent scoreList;
void setUp() throws Exception{
scoreList = FXMLLoader.load(getClass().getResource("/fxml/Score.fxml"));
App.scoreList = new Scene(scoreList,800,500);
}
// Adding data to the Observable List and setting Column Factories
@Override
public void initialize(URL url, ResourceBundle rb) {
data.add(new Player("Chris", 11));
data.add(new Player("Agil", 12));
Nickname.setCellValueFactory(new PropertyValueFactory<Player, String>("nickname"));
Score.setCellValueFactory(new PropertyValueFactory<Player, Integer>("finaleScore"));
table.setItems(data);
}
}
I expected the TableColumn to show the data of the ArrayList
but the TableView
only shows 'no content' in this table.
EDIT Player class
public Player(String nickname, int finalScore){
setNickname(nickname);
setFinalScore(finalScore);
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
void setFinalScore(int score){
finalScore = score;
}
public String getNickname(){
return nickname;
}
public int getFinalScore() {
return finalScore;
}
Upvotes: 1
Views: 710
Reputation: 333
I have tried out your code and this is what I can presume to be the issue:
Score.setCellValueFactory(new PropertyValueFactory<Player, Integer>("finaleScore"));
You have mistype finalScore
as finaleScore
, which returns no values since such a property does not exist. However, this does not affect population of the values for column Nickname. So my second guess is that you may not have set the controller for the fxml file. Please check that in your Score.fxml code you have the line:
fx:controller="<your package name here >.ScoreController"
e.g.
<AnchorPane prefHeight="400.0" prefWidth="600.0" fx:controller="com.example.ScoreController">
<!-- other children nodes here -->
</AnchorPane>
EDIT
Try changing these lines to simply be:
@FXML
public TableView table;
@FXML
public TableColumn Nickname;
@FXML
public TableColumn Score;
Upvotes: 0
Reputation: 506
You could try some changes
Nickname.setCellValueFactory(col ->new SimpleStringProperty(col.getValue().getNickName());
Score.setCellValueFactory(col ->new SimpleIntegerProperty(col.getValue().getFinalScore());
If its working, you have to look at the name convention for cellFactory
Upvotes: 0