Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

My understanding is Cargo Cult Programming!

Parent root = FXMLLoader.load(getClass().getResource("/fxml/home.fxml")); primaryStage.setScene(new Scene(root, 500, 500)); primaryStage.setResizable(false); primaryStage.show();

I have come to the understanding that my understanding of the above code is Cargo Cult Programming.

I am thinking about having an app with TabPane and Tab objects. I was think about having static part which starts with AnchorPane and ends with TabPane. Then there would be a dynamic part that would start with Tab and have a lot of stuff attached. The dynamic part would be created and destroyed dynamically as a result of onAction handlers. But I can't figure out how to do it, and it is because my understanding of the code above is Cargo Cult Programming! What is a "Parent" object exactly? What exactly doe FXMLLoader do?

In order to solve my problem I need to understand the code above in greater depth!

1 Answer

Ok I have solved this problem, but I have to point out that the solution, was from goggling a lot of web pages and not from the information in the pomodoro was taught in a monkey copy fashion. The code I refer to is below. <blockquote> public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
    // Load the font
    Font.loadFont(getClass().getResource("/fonts/VarelaRound-Regular.ttf").toExternalForm(), 10);
    Parent root = FXMLLoader.load(getClass().getResource("/fxml/home.fxml"));
    primaryStage.setScene(new Scene(root, 500, 500));
    primaryStage.setResizable(false);
    primaryStage.show();
}

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

}

</blockquote>

Not enough time was given explaining FXMLLoader.load and so I was clueless about doing in a non-main class situation.

To review the problem I wanted to have a main window with MenuBar and a TabPane where the Tab s are created and destroyed dynamically in response to onAction requests from the user.

The solution is to have 2 fxml files, 2 controllers, Base and Tab with the Tab s created dynamically with FXMLLoader.load.

I had to study the web for days to figure out how to do this. <blockquote> // tell user why he is choosing a file fileChooser.setTitle("Open File for display");

    // run the dialog to get the files
    List<File> fileList = fileChooser.showOpenMultipleDialog( container.getScene().getWindow());

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

    for (File file : fileList) {
        System.out.println(file.toString());

        try {
            Tab tabRoot = (Tab)fxmlLoader.load();

// Tab tabRoot = FXMLLoader.load(getClass().getResource("/fxml/tab.fxml")); TabController mytabController = fxmlLoader.<TabController>getController();

            Tab controlersTab = mytabController.tabController;

            if (tabRoot != controlersTab) {
                System.out.println("not equal tabs");

            }

            // save reference to this Base in the tab's controller
            // so the tab controller can find the controller for its Base
            mytabController.baseOfTab = this;

            // save the model into the tab controller
            // the TabController needs access to the model
            mytabController.model = this.model;

            tabPane.getTabs().add(controlersTab);

// controlersTab.setContent( (Node) FXMLLoader.load(getClass().getResource("fxml/tab.fxml")) );

            mytabController.loadText(file);


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

</blockquote>

The source, which is incomplete, but I have gotten the dynamic tab part working can be found at <a href="https://github.com/pelliott80/peLess">this link a GitHub</a>. <p> Please criticize.