I want to display data from a database within the tableview of the GUI.
I have tried may different ways to no avail. The code runs without errors yet still nothing shows in the table view.
Can you please help clarify my understanding on how to do this.
public class LeaderBoardView implements Initializable {
@FXML private TableColumn<LeaderBoardData, String> colUsername;
@FXML private TableColumn<LeaderBoardData, Integer> colWins;
@FXML private TableColumn<LeaderBoardData, Integer> colWinPercentage;
@FXML private TableView<LeaderBoardData> tableLeaderboard;
private ObservableList<LeaderBoardData> data;
@Override
public void initialize(URL location, ResourceBundle resources) {
colUsername.setCellValueFactory(
new PropertyValueFactory<LeaderBoardData,String>("username"));
colWins.setCellValueFactory(
new PropertyValueFactory<LeaderBoardData, Integer>("wins"));
colWinPercentage.setCellValueFactory(
new PropertyValueFactory<LeaderBoardData, Integer>("winPercentage"));
buildLeaderBoardData();
}
public void buildLeaderBoardData() {
data = FXCollections.observableArrayList();
try {
ResultSet rs = UserDB.getLeaderBoardData();
while(rs.next()){
LeaderBoardData cm = new LeaderBoardData();
cm.username.set(rs.getString("username"));
cm.wins.set(rs.getInt("wins"));
cm.winPercentage.set(rs.getInt("winPercentage"));
data.add(cm);
//System.out.println(data.get());
}
tableLeaderboard.setItems(data);
} catch(Exception e) {
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
public class LeaderBoardData {
public SimpleStringProperty username;
public SimpleIntegerProperty wins;
public SimpleIntegerProperty winPercentage;
// public UserData(String username, int wins, int winPercentage) {
// this.username = new SimpleStringProperty(username);
// this.wins = new SimpleIntegerProperty(wins);
// this.winPercentage = new SimpleIntegerProperty(winPercentage);
// }
public String getUsername() {
return username.get();
}
public Integer getUserWins() {
return wins.get();
}
public Integer getWinPercent() {
return winPercentage.get();
}
}
Expected result - to get the table view to display a Leader Board of Users, Wins, Win Percentage