Loading multiple fxml in javafx
Asked Answered
H

2

4

I've been searching for a while and I haven't been able to get what I wanted to do. I've been using the javafx framework to switch fxml in javafx in this thread 'Loading new fxml in the same scene' with some success. I mean, I have a main window, and this window have two different areas, the MAIN area, and the CONTENT area.

The MAIN area will remain the same in the whole application, but the CONTENT area, will be changing constantly. My MAIN area has just a toolbar, and depending which button in the toolbar you click, the fxml (and the behavior) in the CONTENT area will change.

I already have that behavior and it works like a charm using that said framework.

The thing is, that I want that all my CONTENT areas could have their own MAIN areas too, and an own CONTENT areas too. And depending on what you do in that second MAIN areas, the fxml and the behavior in that second CONTENT areas changes too.

I don't know if the framework I'm using is enough and if I will be able to get what I want to do using it.

Do you know another different framework that could help me to get that functionality?

Thanks a lot in advance!

Hyde answered 16/11, 2017 at 10:24 Comment(2)
It's not really clear what you're asking here. Have you tried using the approach that already works for you for this scenario? If so, does it work, and if it doesn't, what specifically goes wrong?Hushaby
It doesn't work, because the framework has all its attributes and methods static. And it has to be initialized from a JavaFX application class, and when you swap fxml and "import" a new child fxml, it's not a JavaFX application itself, so you can't initialize a new framework instance from there. And you can't use the original framework instance because you'd override the previous values on it. I'll try to edit my question as soon as I can to try to clarify it.Hyde
H
4

Every .fxml that you load can have its own controller. Every controller can have custom action and loading code just like your main one does.

Basically, JavaFX is modular. If something is possible in your main pane, then it's possible in all its children too.

Generally, in UI-based frameworks, it is a wise idea to keep a hierarchy of controllers that mirrors the hierarchy of their respective components. If you want to get the child controller for the child element (for example, in order to pass the reference to the parent controller), then you can't rely on the FXML.load() shortcut, you have to use the longer syntax of:

FXMLLoader loader = new FXMLLoader(getClass().getResource("your.fxml"));
YourPaneType pane = loader.load();
YourControllerType controller = loader.getController();
Hydrate answered 16/11, 2017 at 10:33 Comment(0)
G
1

You can load all of your content using fx:include and make them visible based on your needs.

<AnchorPane fx:id="root">
    <children>

        <!-- initial content -->
        <fx:include fx:id="content1" source="content1.fxml" />

        <!-- other content we want to pre-load -->
        <StackPane visible="false" managed="false">
            <children>
                <fx:include fx:id="content2" source="content2.fxml" />
                <fx:include fx:id="content3" source="content3.fxml" />
            </children>
        </StackPane>

    </children>
</AnchorPane>

and in the controller

@FXML
Pane root;

@FXML
Pane content1;

@FXML
Pane content2;

@FXML
Pane content3;

@FXML
NestedContentController content1Controller;

@FXML
NestedContentController content2Controller;

@FXML
NestedContentController content3Controller;

public void showContent1() {
    root.getChildren().setAll(content1);
}

public void showContent2() {
    root.getChildren().setAll(content2);
}


public void showContent3() {
    root.getChildren().setAll(content3);
}

(The initially hidden content could also be defined outside the object hierarchy in an <fx:define> element, but then SceneBuilder doesn't provide an option to reveal the included file.)

Groveman answered 30/5, 2022 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.