【发布时间】:2019-03-28 10:22:39
【问题描述】:
在我的第一个场景中,用户从表格中选择了一个团队,接下来是关于该团队的信息。 然而,我的问题是将字符串(存储为标签)传递到显示所选团队结果的下一个场景中。
public void goResults(ActionEvent event) throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("TeamResults.fxml"));
Parent tableViewParent = (Parent)loader.load();
Scene tableViewScene = new Scene(tableViewParent);
//access controller and call method
TeamResultsController controller = loader.getController();
controller.initData(teamLabel.getText());
这是我在选择结果按钮时将团队名称 (teamLabel) 从场景 2 传递到场景 3。
private String team;
public void initData(String teamName) {
this.team = teamName;
这是将标签值传入的函数,并将其设置为字符串团队。
但是,如果我尝试在此函数之外打印团队变量,则将其设置为 null。我如何让它保持 initData 之外的值?
例如:
public void initialize(URL url, ResourceBundle rb) {
try {
Connection con = DBConnector.connect();
ResultSet rs = con.createStatement().executeQuery("SELECT DISTINCT t1.title AS title, t2.title AS title2, g.score1, g.score2 FROM games g INNER JOIN teams AS t1 ON g.team1_id = t1.id INNER JOIN teams AS t2 ON g.team2_id = t2.id WHERE g.team1_id ="+team +"OR g.team2_id ="+team+"");
while(rs.next()){
reslist.add(new TeamResultsTable(rs.getString("title"), rs.getString("title2"), rs.getInt("score1"), rs.getInt("score2")));
}
} catch (SQLException ex) {
Logger.getLogger(TableController.class.getName()).log(Level.SEVERE, null, ex);
}
title.setCellValueFactory(new PropertyValueFactory<TeamResultsTable, String>("title"));
title2.setCellValueFactory(new PropertyValueFactory<TeamResultsTable, String>("title2"));
score1.setCellValueFactory(new PropertyValueFactory<TeamResultsTable, Integer>("score1"));
score2.setCellValueFactory(new PropertyValueFactory<TeamResultsTable, Integer>("score2"));
gameTable.setItems(reslist);
我正在尝试将它传递给这个查询(暂时不用担心准备好的语句)。
你会推荐什么方法?我知道这应该与其他示例线程相似,但我尚未将其与我的相关联。对于我的经验不足,我深表歉意,但某种框架会令人难以置信。
但是任何帮助都很棒, 谢谢。
【问题讨论】: