I am trying to add x number of buttons to a VBox located in a scrollpane. The Vbox, and scrollpane are made with scenebuilder.
However i can't seem to connect the VBox to the controller as a variable.
The piece of code that spawns the scene is the following:
Stage stage = MyMain.stage;
stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("/main/fxml/catEdit.fxml"))));
stage.show();
My FXML is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane maxHeight="-Infinity"
maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="400.0"
prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.111"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="main.controllers.CatEditCon">
<center>
<ScrollPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0">
<children>
<VBox fx:id="catBox" alignment="CENTER" prefHeight="200.0" prefWidth="100.0" />
</children></AnchorPane>
</content>
</ScrollPane>
</center>
</BorderPane>
And the controller is:
package main.controllers;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import main.MyMain;
import main.database.master.Category;
import main.fxml.custom.CatBut;
public class CatEditCon{
@FXML public VBox catBox;
public CatEditCon(){
catBox = new VBox();
updateScroll();
}
public void updateScroll(){
Category cat = MyMain.baseData.getCategories();
System.out.println(catBox);
int len = MyMain.baseData.getArticles().size();
for(int i = 0 ; i < len; i++ ){
System.out.println(""+i);
Button b = new Button();
catBox.getChildren().add(b);
}
}
}
Any tips to what i do wrong? The scene opens, and the sysout counts upwards as it should. But no buttons appear? If the catBox isnt initialized in the constructor, i get a nullPionter to that object. So it is as if i don't make the connection to the fxml object.