1

I cannot figure out why my custom Stackpane does not appear. I have exported it to scenebuilder and scenebuilder also does not show my items in stack pane. Also i set blue background and it worked. Everything went blue. When i include my fxml directly, it works correctly but not when i use custom component aproach. I managed to get my button showing when it was not in pane. Is this layout issue or am i missing something.

public class TestPane extends StackPane{

public TestPane() {

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
            "/fxml/components/TestPane.fxml"));

    //Set controller fot opened dialogWindow
    fxmlLoader.setController(this);

    try {
        fxmlLoader.load();
    } catch (IOException exc) {
        System.out.println("Error: " + exc.toString());
    }
}

TestPane

    <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button mnemonicParsing="false" text="Button" />
</children>
</StackPane>

TestPane in scenebuilder

Testing testpane in empty scenebuilder

Hillar Kapsta
  • 135
  • 1
  • 8
  • 2
    Are you trying to follow [this pattern](https://docs.oracle.com/javase/9/docs/api/javafx/fxml/doc-files/introduction_to_fxml.html#custom_components)? If so, you need to use the "dynamic root", as shown in the documentation. If not, you are simply loading the `StackPane` defined in the FXML, and not doing anything with it, so it never appears anywhere. – James_D Apr 07 '18 at 16:36
  • I had to use "dynamic root" approach. Thank you very much. You are an hero! Another question, does this override my old root if i use that in my root fxml ? – Hillar Kapsta Apr 07 '18 at 17:02
  • Not sure I understand that question. `fxmlLoader.setRoot(this)` means `this` (i.e. the current `TestPane`) is the root element of the FXML. So then the child nodes (the `Button` in this example) are added as children of the current `TestPane`, which is what you want. – James_D Apr 07 '18 at 17:03
  • 1
    Ignore my last question. It seems i had wrong idea about root element. I'm really reallty new to JavaFx. My idea was that root element meant root wrapper of whole project and not a single file. To anyone who needs more info on fx root, i have a link: [link]https://stackoverflow.com/questions/23600926/how-to-understand-and-use-fxroot-in-javafx . – Hillar Kapsta Apr 07 '18 at 17:14
  • I'd forgotten about that question/answer: thanks for linking it. I'll mark this question as a duplicate of that, since I think it describes everything you need to solve this issue, and will be a better way of directing other users who see this question to that one. – James_D Apr 07 '18 at 17:16

0 Answers0