Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Can't access to elements uploaded from a FXML file in another FXML scene  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to do something like this but dynamically.

I have a scene "Configuration.fxml" and a "ControllerConfiguration.java" and I would like to add in his GridPane "gp" the "ESP_button.fxml" and read the textfields after they are modified. ( Pictures below)

So I succeded adding many rows with the same ESP_button.fxml but I can't access to their text fields, because I get a null PointerException.

I tryed also to create a loader for ESP_button and a Controller in order to receive the TextField from the controller of ESP_button, but I still receive a NullPointerException.

This is my code: ConfigurationController.java


Configuration.fxml


ESP_button.fxml


I would like to call on X, Y and mac textProperty() in order to attach them the listener, but I realized that X, Y and mac are null so I can't proced.
EspButton :

Configuration:


This is a part of the Stacktrace I consider important:

 
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Salvatore Gurzì!

We will need to see the stack traces of the errors you get.  When you post something like, "I get the error x", be sure to include the stack trace for x.

I'm curious though.  Why are you trying to do this with FXML files?  Wouldn't a class work better?
 
Salvatore Gurzì
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing so because I don't know how to create a row with the button with those distances and I thought using SceneBuilder to create the gridPane I wanted would have beeen better and faster.

So, now I don't have any interruption but if in ConfigurationController I add X.setText("argo") after if(X ==null) System.out.println... I get the stack trace that I added in the post
I think also mught help looking at the all code here in github

 
Knute Snortum
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you're adding new information, make a new post; don't edit your original post except for small, clarifying changes.
 
Knute Snortum
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the classes in GitHub are not the ones you executed because the exception line numbers don't match.  But if I had to guess, I would think that the FXML file isn't getting loaded and therefore the gp variable isn't getting instantiated.
 
Salvatore Gurzì
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did small changes and I hadn't pushed them, check the git hub now please.
 
Salvatore Gurzì
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well is not like that because as you see from the immage I am able to put it in the scene. Anyway I pushed the modification, take a look at it if you can... thank you
 
Greenhorn
Posts: 19
1
MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@FXML GridPane gp is null because:
1. GridPane with fx:id = gp is in ESP_Button.fxml
2. associated controller class for ESP_Button.fxml is ButtonController
3. When ConfigurationController initializes it cannot find an associated GridPane and ignores
4. you have to take the 2 controller classes and inject the dependencies between those. Then you can work inside ConfigurationCOntroller with something like myInjectedButtonController.getGridPane().add(stuff)
 
Salvatore Gurzì
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you show my an example? Because I tryed that way ( this is why there is a ButtonController) but I didnt succed... in any case thank you for the help.
 
Jan Daermann
Greenhorn
Posts: 19
1
MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are multiple ways for DI. This one is specific to JavaFX Controllers and only one of multiple solutions. The only new thing is that you have to wrap your head around the fact that there is only one instance of the FxController that works with the corresponding fxml file -> the one that the fxml file (really:FXMLLoader) instantiated for itself.

Let's assume you have classes FxControllerOne (for FXML1.fxml) and FxControllerTwo (for FXML2.fxml). Let's assume you have info from FXML2.fxml that you read out in its Controller FxControllerTwo and you want to transfer that to FxControllerOne in order to get it onto FXML1.fxml:

1. Create a reference for FxControllerOne in class FxControllerTwo:
FxControllerOne FxC1;

2.Create a method in class FxControllerTwo:
public static void injectFxControllerOne(FxControllerOne fxc1){this.FxC1 = fxc1};

3. In the @FXML private void initialize() method in class FXControllerOne add the line
FxControllerTwo.injectFxControllerOne(this);

Now you should be good to call any method of FxControllerOne inside of FxControllerTwo with the following:
FxC1.doAnyFXC1Method();

'hope that helps (More than it confuses).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!