0

I want to add buttons to created HBox, in fxml. Everything has been drawn, and I'm collecting some data from database, creating buttons, but when I try to add it tho said HBox, it won't add them.

FXMLLoader myLoader = new FXMLLoader(getClass().getResource("/fxml_cart.fxml"));
AnchorPane root = (AnchorPane) myLoader.load();
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.resizableProperty().setValue(Boolean.FALSE);
Main.getMainStage().close();
Main.setMainStage(stage);
List<Products> productsList = MySQLOperations.getProductsListByCategory("1");
List<ProductCategory> productCategory = MySQLOperations.getProductCategoryList();
System.out.println(productCategory.size());
List<Button> listProductCategory = new ArrayList<>();
HBox listCategoryHBox = new HBox();
for (ProductCategory pc : productCategory) {
    Button productCategoryBtn = new Button();
    productCategoryBtn.setText(pc.getCategoryName());
    // productCategoryBtn.setOnAction(value);
    productCategoryBtn.setId("productCategoryBtn_" + pc.getKeyProductCategory());
    productCategoryBtn.setPrefWidth(88);
    productCategoryBtn.setPrefHeight(32);
    listProductCategory.add(productCategoryBtn);
}
listCategoryHBox.getChildren().addAll(listProductCategory);
CartController fxmlCont = (CartController) myLoader.getController();
fxmlCont.setProducts(listCategoryHBox);
stage.show();

I even created some temp HBox, so I can somehow push it in. In CartController are controllers for fxml_cart.fxml, and in it is

@FXML
private HBox products;

on witch I call fxmlCont.setProducts(listCategoryHBox);

Marko Adam
  • 107
  • 2
  • 12
  • 4
    Just pass the data. Then do all the other stuff in `CartController`. – SedJ601 Jan 22 '18 at 21:34
  • 1
    You should call methods on the controller to do this. See https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml for various ways to access the controller after loading the FXML – James_D Jan 22 '18 at 21:46

0 Answers0