4

I'm using javaFX. I have an FXML layout with no controller that includes two layouts like so

<VBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <StackPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
            <fx:include source="/views/partials/message.fxml" /> 
            <fx:include source="/views/common/Customer.fxml" />
        </StackPane>
    </children>
</VBox>

Each of the nested (FXML) have their own controller. The parent VBOX is loaded and rendered by a third class (not a dedicated controller).

I want to be able to access the controller for the message.fxml from external controllers. So I did the following.

FXMLLoader messageLoader = new FXMLLoader();
messageLoader.setLocation(getClass().getResource("/views/partials/message.fxml"));
Pane pane = messageLoader.load();
MessageController messageController = messageLoader.getController();

but now my problem is that the controller is not executing changes (e.g setting text and making visible or invisible) that I make on the 'message.fxml' even though I can see by using System.out.printLn that the messageController.anyMethod() are being executed but it does not appear to show any results on my layout when I run the app.

Is there a solution for this??

Suemayah Eldursi
  • 969
  • 4
  • 14
  • 36
  • 3
    To access the controller for the included FXML (the "nested controller") in the controller for the main FXML, see the [documentation](http://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#nested_controllers), or [this question](http://stackoverflow.com/questions/23592148/javafx-nested-controller) (among others). It's quite hard to see a reasonable scenario where you would want access to the nested controller anywhere other than in the "main controller": probably your best option is to provide a main controller and move the relevant functionality to it. – James_D Nov 16 '16 at 13:11
  • See an answer [here](https://stackoverflow.com/a/59552853/3992939) – c0der Jan 01 '20 at 13:20

0 Answers0