【发布时间】:2019-09-12 23:35:37
【问题描述】:
我想在 GUI 的 tableview 中显示数据库中的数据。
我尝试了可能不同的方法都无济于事。代码运行没有错误,但表格视图中仍然没有显示任何内容。
您能否帮助澄清我对如何执行此操作的理解。
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();
}
}
预期结果 - 让表格视图显示用户、获胜、获胜百分比的排行榜
【问题讨论】:
标签: java javafx tableview fxml