1

I'm new to JavaFX and I'm trying to dynamically add buttons to a TilePane based off data stored in a database. For each record that gets returned from the database I'll add a new button the TilePane (FXML of button is listed below). Then this button needs to ben changed on the data stored in the database.

I try to lookup a label that's 2 layers down in the button (child of a Vbox that is placed in the button) it returns null.

What am I doing wrong? I tried to do a Lookup from the Vbox, because i tought that it maybe didn't work beacuse it's a child of a child. Unfortunately this didn't help.

try {
    while (res.next()){
        addActuator(); //adds the button

        Button button = (Button) Actuator_Tile_Pane.lookup("#placeholder");
        Label label = (Label) button.lookup("#label"); // returns null

        button.setId(res.getString("id"));
        System.out.println(label);
    }
} catch (SQLException e) {
    System.out.println(e.getMessage());
}
<Button fx:id="placeholder" mnemonicParsing="false" prefHeight="150.0" prefWidth="150.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
    <graphic>
        <VBox fx:id="vbox" alignment="CENTER" prefHeight="200.0" prefWidth="100.0" spacing="10.0">
            <children>
                <Label fx:id="label" text="Label" />
                <ImageView fx:id="img" fitHeight="73.0" fitWidth="98.0" pickOnBounds="true" preserveRatio="true">
                    <image>
                  <Image url="@../icon/lightbulb-on-outline.png" />
                    </image>
                    <effect>
                        <ColorAdjust brightness="-0.12" hue="0.29" saturation="1.0" />
                    </effect>
                </ImageView>
                <ToggleButton fx:id="toggle" blendMode="SRC_ATOP" mnemonicParsing="false" text="ToggleButton">
                    <effect>
                        <ColorAdjust />
                    </effect>
                </ToggleButton>
            </children>
        </VBox>
    </graphic>
    <padding>
        <Insets left="20.0" right="20.0" />
    </padding>
</Button>
itschiel
  • 11
  • 1
  • 2
    Why use lookups? Just inject the `label` directly into the controller and access it there. – James_D May 06 '20 at 15:03
  • The controller is connected to the main scene. The FXML listed here is loaded into the main scene on initiation. As far as I know, its then not possible to specify the labels in the controller. If it is, i would like to know how. – itschiel May 06 '20 at 15:22
  • 2
    [mcve] please .. and stick to java naming conventions. – kleopatra May 06 '20 at 15:22
  • have a look at [How to pass parameters between controllers (FXML)](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – kleopatra May 06 '20 at 15:24
  • Specify a controller for the FXML you show here, put the code in that controller - either in the `initialize()` method or in a method you invoke (I don't quite understand the structure you're describing, but it's always possible to do this). As suggested, create a [mre] which demonstrates the problem. – James_D May 06 '20 at 15:25
  • 4
    Bottom line: lookups only work after a CSS pass has been made, and you have little control over when that happens. Don't use CSS lookups for nodes you define in FXML files. – James_D May 06 '20 at 15:26
  • Tanks James and Kleopatra. I will ferther into how to pass paratmeters between controllers. – itschiel May 06 '20 at 16:17

0 Answers0