-2

The best article I found was: How to create multiple javafx controllers with different fxml files?

However im really confused on how this works. All examples just seem a bit too complex for the initial learning.

So here I have a simple helloWorld for testing purposes. As you can see in the xml, I have a container, menu and footer. However, I want all 3 of them to have seperate controllers and XML files which are then merged and shown as seen in the XML below after the class:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;



public class HelloWorld extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        FXMLLoader loader = new FXMLLoader();
        Parent root = loader.setLocation(getClass().getResource("main.fxml"));
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();

        MainController mainController = loader.getController();
    }
}

XML

<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.canvas.*?>

<HBox fx:id="container" id="container" fx:controller="core.GuiController" xmlns:fx="http://javafx.com/fxml">
    <HBox fx:id="post" id="post">
        <!-- Stuff -->
    </HBox>

    <HBox fx:id="friends" id="friends">
        <!-- Stuff -->
    </HBox>

    <HBox fx:id="profile" id="profile">
        <!-- Stuff -->
    </HBox>
</HBox>

I could really benefit from a simple example. How can I keep them in seperate files and merge them while they each retain their own controllers?

10
  • Why do you want each item to have its own controller? Sep 27, 2016 at 19:28
  • @MichaelPickett check the xml again (edited). These are 3 important functionalities and I want to isolate them with their own controllers and view. I just want to break everything down.
    – Asperger
    Sep 27, 2016 at 19:37
  • You might be able to make a root node, load each of FXML files, and add them to the root. Then show the root stage. This might work. Sep 27, 2016 at 19:39
  • The link you looked at is exactly what you want to do. Sep 27, 2016 at 19:44

1 Answer 1

0

You could follow this tutorial

public class MainApp extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("AddressApp");

        initRootLayout();

        showPersonOverview();
    }

    /**
     * Initializes the root layout.
     */
    public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Shows the person overview inside the root layout.
     */
    public void showPersonOverview() {
        try {
            // Load person overview.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
            AnchorPane personOverview = (AnchorPane) loader.load();

            // Set person overview into the center of root layout.
            rootLayout.setCenter(personOverview);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns the main stage.
     * @return
     */
    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

In this example You have two fxml files, RootLayout.fxml and PersonOverview.fxml. You set the scene of your primarystage to (BorderPane)RootLayout.fxml then add PersonOverview.fxml to the BorderPane.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.