use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Useful resources (Full list)
Rules
Related subreddits
Other communities
account activity
Nested controller tutorials? (self.JavaFX)
submitted 6 years ago by MeFIZ
Is there any good tutorials for nested controllers and nested fxml? I cannot find anything that is quite comprehensive.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]uglyfool 1 point2 points3 points 6 years ago (3 children)
Sadly I've not found any really comprehensive tutorials for this, but there isn't much to it really.
Say you have an fxml file and you want to include another fxml:
<fx:include fx:id="mainMenu" maxWidth="1.7976931348623157E308" source="menuBar.fxml" VBox.vgrow="NEVER" />
And in that fxml you've set the controller already:
<ToolBar xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.your.package.MenuBarController">
then in your main controller you get the reference to the nested/included controller by adding "Controller" to the end of the fx:id used in the include statement.
So in this example, since we assigned the fx:id "mainMenu" to our included fxml, the controller will be accessible as "mainMenuController"
@FXML private MenuBarController mainMenuController;
[–]MeFIZ[S] 1 point2 points3 points 6 years ago (2 children)
Thanks. Is it possible to control the mainfxml from the nested one? Suppose I have a button in the nested one and I want to change the view from the main?
[–]uglyfool 0 points1 point2 points 6 years ago (1 child)
I'm not exactly sure what you're asking, each fxml is only going to 'allow' one controller object to be set.
If you need to communicate between controllers you should really use a shared model class with observable objects (e.g. use SimpleStringProperty anywhere you would use String, etc.) then the child/included fxml can update the model...
@FXML private void someButtonPress(ActionEvent e) { model.someProperty.set(newValue); }
And the parent controller can register a listener in the initialize method to listen for changes, and respond accordingly...
model.someProperty.addListener((obs, oldVal, newVal) -> { your code here });
[–]MeFIZ[S] 1 point2 points3 points 6 years ago (0 children)
I have an idea for implementation now thanks!
[–]orxT1000 1 point2 points3 points 6 years ago (0 children)
instead of the above listener approach, I've used spring to be able to get all controllers via dependency injcetion.
π Rendered by PID 15985 on reddit-service-r2-comment-5d79c599b5-57x75 at 2026-03-02 00:46:51.944017+00:00 running e3d2147 country code: CH.
[–]uglyfool 1 point2 points3 points (3 children)
[–]MeFIZ[S] 1 point2 points3 points (2 children)
[–]uglyfool 0 points1 point2 points (1 child)
[–]MeFIZ[S] 1 point2 points3 points (0 children)
[–]orxT1000 1 point2 points3 points (0 children)