Skip to content

alpha-mo/JavaFx-multiple-Controllers-communications-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaFx multiple Controllers communications. – (tutorial)

The example shows how to establish communication between multiple Controllers assuming that the application is built on a FXML GUI that includes other nested FXML files.

What do we want to achieve:

  • Inject sub FXML files into the parent FXML.
  • Create instance of the controllers into each of the other ones.
  • Call methods that exist in a controller from another one.

Solution:

Since we are using nested FXML files as shown in the picture here: We need to give an fx:id to the pane holding all the contents of each sub FXML file, this fx:id will be added manually (unfortunately) to the parent FXML file:

<fx:include fx:id="right" source="RightPane.fxml" />

This will allow the injecting of the sub FXMLs Controllers into the Main Controller:

@FXML private LeftPaneController leftController;

All what’s left is just to create public methods in the child Controllers to inject the desired Controller, and call these methods at the initializing of the main Controller:

@FXML private void initialize() {
		leftController.injectRightPaneController(rightController);
		rightController.injectLeftPaneController(leftController);
	}
private RightPaneController rightPaneController;
	
	public void injectRightPaneController(RightPaneController rightPaneController) {
		this.rightPaneController = rightPaneController;
	}

Demo:

About

JavaFx multiple Controllers communications tutorial

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages