4

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
Danfjo
  • 73
  • 2
  • 7
  • Implement `Intitializable`. At the time your Controller is created, the @FXML values are not injected. You have to put all the code that needs FXML Values at Startup in the `initialize` method from that Interface – Felix Jun 26 '17 at 18:14
  • I tried around with that before, too. If i make: @Override public void initialize(URL location, ResourceBundle resources) { catBox = new VBox(); } i get a nullPointerException on this line: `catBox.getChildren().add(b);` – Danfjo Jun 26 '17 at 18:17
  • You don't have to create a new VBox. It's in your FXML, isnt it? Wait for the FXMLLoader to inject the VBox to your Controller. When finished, the initialize method will be invoked and you can add the Buttons to that – Felix Jun 26 '17 at 18:20
  • 3
    Do not initialize the injected fields. Do not try to use injected fields in the constructor. Use one of the approaches described here: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian Jun 26 '17 at 18:25
  • It worked! Perfect, thank you very much @rollback. I thought that the initialize was used to connect the fxml to the controller, and not that it meant that it's only ready by then. Houurs of frustrations are over. :-D – Danfjo Jun 26 '17 at 18:27

0 Answers0