Avatar of Panos
PanosFlag for Germany

asked on 

Javafx passing parameters before loading the fxml loader

Hi experts . I m trying to pass parameters to fxml loader before loading the controller to define the gui how it should look like and what it should include a.s.o.
I have a main container and after clicking on buttons this container is loading the data from fxml. like this:
@FXML
private StackPane acContent;
@FXML
private void btnSupplierOnClick(ActionEvent event) throws IOException {
  FXMLLoader fxmlLoader = new FXMLLoader();
  fxmlLoader.load(getClass().getResource(/view/application/supplier/ViewSupplierList.fxml").openStream());
  AnchorPane acPane = fxmlLoader.getRoot();
  ViewSupplierListController supplierListController = fxmlLoader.getController();
  // Passing data to the supplierListController
  supplierListController.setData(userNameMedia,rootPane,acContent,acMain);
  acContent.getChildren().clear();
  acContent.getChildren().add(acPane);
}

Open in new window

With the setData function i m passing values to the controller but this happens after the controller has been loaded.
After googling i found a solution here (1. Create a controller instance manually) but i didn t get it to work.
Any help to solve my problem?
Thank you in advance.
Java

Avatar of undefined
Last Comment
Panos
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

After googling i found a solution here (1. Create a controller instance manually) but i didn t get it to work.

Did you use the first example? The second one is wrong, I think
Avatar of Panos
Panos
Flag of Germany image

ASKER

Hi
I have use the first example but i have an problem to set the acPane.  
Avatar of Panos
Panos
Flag of Germany image

ASKER

Finally i got it.   This is the code:
1)
private StackPane acContent;
@FXML
private void btnSupplierOnClick(ActionEvent event) throws IOException {
  FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/application/supplier/ViewSupplierList.fxml"));
  // Create a controller instance
  ViewSupplierListController supplierListController = new ViewSupplierListController(userNameMedia,rootPane,acContent);
  // Set it in the FXMLLoader
  fxmlLoader.setController(supplierListController);
  AnchorPane acPane = fxmlLoader.load();
  acContent.getChildren().clear();
  acContent.getChildren().add(acPane);
}

Open in new window

2) Remove the  fx:controller= .....
in the fxml datei
3) Create a constructor:
public  ViewSupplierListController(UserNameMedia data, StackPane rootPane,StackPane acContent) {
         this.userNameMedia = data;
         this.rootPane = rootPane;
         this.acContent = acContent;

    }

Open in new window

The mistake i did was instead of
AnchorPane acPane = fxmlLoader.load();

Open in new window

i still used:
AnchorPane acPane = fxmlLoader.getRoot();

There is also a very useful link ​here​​​ for passing information between scenes.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

OK. Glad you sorted it out
ASKER CERTIFIED SOLUTION
Avatar of Panos
Panos
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo