-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?

Community
  • 1
  • 1
Asperger
  • 3,064
  • 8
  • 52
  • 100
  • Why do you want each item to have its own controller? – Hypnic Jerk Sep 27 '16 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 '16 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. – Hypnic Jerk Sep 27 '16 at 19:39
  • Possible duplicate of [How to create multiple javafx controllers with different fxml files?](http://stackoverflow.com/questions/19342259/how-to-create-multiple-javafx-controllers-with-different-fxml-files) – Hypnic Jerk Sep 27 '16 at 19:42
  • The link you looked at is exactly what you want to do. – Hypnic Jerk Sep 27 '16 at 19:44
  • @MichaelPickett indeed but somehow I dont get it. I really dont : ( – Asperger Sep 27 '16 at 19:48
  • Read up on these docs: http://docs.oracle.com/javafx/2/api/javafx/fxml/doc-files/introduction_to_fxml.html#include_elements and http://docs.oracle.com/javafx/2/api/javafx/fxml/doc-files/introduction_to_fxml.html#nested_controllers – Hypnic Jerk Sep 27 '16 at 19:50
  • "I don't understand this post/article/documentation" is not really a question. You just have to read it and figure it out. No-one can understand it for you. Try the code, experiment with changing it and see what happens, etc etc. You can also read [this question](http://stackoverflow.com/questions/23600926/how-to-understand-and-use-fxroot-in-javafx/23601288#23601288) if you want to use ``. Note you can also do what you are asking with [``](http://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#nested_controllers) – James_D Sep 28 '16 at 01:10
  • @James_D I did read the docs and I have understood as much as that, its just that I couldnt manage to get things working after trying a few solutions so that I can analyse it. There are situations wheres its easier to see how people have "glued" it together. My question is on the very bottom. – Asperger Sep 28 '16 at 04:48
  • @James_D wow your post in the link is really great. You always have quality educational answers on stackoverflow. – Asperger Sep 28 '16 at 04:50

1 Answers1

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.

bshek
  • 109
  • 8