我在点击一个按钮时从db表中获取行数,并且根据计数,我在UI中添加了已存在于UI中的按钮。该循环运行正常,但我的问题只有一个按钮,循环中的最后一个条目被添加到ui。
单击Called Function按钮:
@FXML
public void goToProjectUpdateScreen(ActionEvent event) {
Session session = NewHibernateUtil.opensession();
session.beginTransaction();
Query queryResult = session.createQuery("from Project where RegistrationId= 3");
java.util.List allUsers;
allUsers = queryResult.list();
for (int i = 0; i < allUsers.size(); i++) {
try {
Project project = (Project) allUsers.get(i);
Button button = new Button();
button.setOnMouseClicked((MouseEvent t) ->
{
System.out.println(button.getId());
});
button.setText(project.getProjectname());
button.setId(Integer.toString(project.getProjectid()));
showProjectUpdateDialog(button,project.getProjectname());
} catch (IOException ex) {
Logger.getLogger(NewProjectScreenController.class.getName()).log(Level.SEVERE, null, ex);
}
}
session.getTransaction().commit();
session.close();
}
public void showProjectUpdateDialog(Button button,String buttonname) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ProjectUpdateScreen.fxml"));
Parent root = (Parent) fxmlLoader.load();
ProjectUpdateScreenController controller = fxmlLoader.<ProjectUpdateScreenController>getController();
controller.setButton(button,buttonname);
fxmlLoader.setController(button);
MainController.parentWindow.getScene().setRoot(root);
}
项目更新屏幕中的SetButton函数:
@FXML
public HBox projectlist;
public void setButton(Button button ,String buttonname) {
projectlist.setSpacing(10);
projectlist.getChildren().add(button);
}
任何人都可以解释一下这个???
答案 0 :(得分:1)
您必须只加载一次fxml。目前,每次添加按钮并将其设置为root时都会加载它,因此最后一个是剩下的按钮。