i have spent at least 8 hours, trying tons of things, going in possibly ALL posts about similar things on google and stackoverflow, the same null and thread errors etc, now IT WORKS !!!!!!!!!!!!!
More Gotchas that took me 1.5 hours to find out: - Classes have to have the same name as FXML Files (you can append "Controller" to the name) - FXML files names have to be apparently capitalized - Dont remember if he said it but the ID of the FXML top container has to be the same as its name and as the one referenced in the main FXML. For anyone who is still struggling xD (maybe he said all of that and I am just stupid xD)
6:53 There is no naming convention there, but in the main controller class when dependency injecting the other controllers: @FXML ChildController Controller; i.e. you can mak make fx:id="babana" then the controller variable must be named bananaController
6:53 There is no naming convention there, but in the main controller class when dependency injecting the other controllers: @FXML ChildController Controller; i.e. you can mak make fx:id="babana" then the controller variable must be named bananaController
What a great tutorial! I finally dove head first in this since my controller class now has 1400 lines and fxml more than 250 lines. It was time to start cleaning that up.
Any reason why you created your controllers manually and not while creating the FXML files? For the latter one, you get the chance to create the controller together with the FXML file (and even a corresponding CSS file, if you want) while walking through the FXML file creation process wizard (at least, in NetBeans it's like that). The small advantage of this is that the template already adds `implements Initializable` to the class and also adds the method `initialize`...
+dony kisworo, Sorry for the late reply but RUclips flagged your comment as Spam! Its strange since your comment is just fine. Anyways ... What you need is a reference to the Controller from that other window/Scene (which has that button you want to disable) to be present in whatever code you are trying to do that from. My JavaFX/Spring tutorials show you how to Dependency Inject (DI) a @Controller to allow you to do that. Thanks for your comment.
awesome demo, just too complicated for a beginner, I am still struggling to get this to work, created a simple project with 1 scene, created 2 fxml files, one for the main and one for the child, the child has a label in it and the main has the child and a button, my challenge to myself is to get the button in the main to change the label in the child, so far .... absolutely no luck. used scene builder as you did
its kinda weird ... why do we have to reinject the controller if its specified in the fxml the controller in the first place ... have they patched this ? I think this is clearly a bug in java fx
Hi, thanks for the tutorial. I've a little problem when i want to write in my textarea with events. (log textArea) I've a stage with 1 TabPane (with 2 tabs) in the first tab(LogController), i have a textArea with a button (method createLog), and in the second tab (Tab2controller) i have a simple scene with pane and there's is a button i'd like write each event cliked into the textArea. For example, when i click in the button in second tab,the event : javafx.event.ActionEvent[source=Button[id=bttnCreate, styleClass=button]'Create'] I can retrieve by getText method in my LogController In Tab2Controller: @FXML public void toScene1(ActionEvent event) throws IOException { System.out.println("Bouton Scene 1 cliqué"); FXMLLoader loaderLog = new FXMLLoader(); loaderLog.setLocation(LogTabController.class.getResource("LogTab.fxml")); loaderLog.load(); LogTabController logTabController = loaderLog.getController(); // logTabController.createLog(event); // logTabController.monLog(event); TextArea area = logTabController.getLogTextArea(); // logTabController.setLogTextArea(area); // area.textProperty().bind(bttnScene1.textProperty()); logTabController.addLog("bob"); in LogController: @FXML void createLog(ActionEvent event) { this.event = event; logTextArea.setText("Oiiiiii "); logTextArea.appendText("Ok "); if (event != null) { logTextArea.appendText(event.toString() + " "); } System.out.println("Loooggg"); System.out.println(event); } void addLog(String text) { logTextArea.clear(); logTextArea.appendText(text + " "); logTextArea.requestFocus(); logTextArea.setPromptText("qsqsqsqqsqq"); System.out.println(logTextArea.getText()); createLog(event); } Thanks for your help
Hi +Surfeur, thanks for your comment. I am not ignoring it but unfortunately, I am too busy to look at your request.. I am very sorry :( I will try to come back to your comment if time permits.
Man. I just CANT get multiple fxml controllers to work. Even following this video I still get nullpointer any time I try to change an object (like .setText) I'm running everything on a single controller now and it's getting really annoying navigating a 2500 line Class file
Hi +Matthew, sorry to hear you had issues. Glad to hear you got it resolved though. If you feel your solution could help others please share it here in the comments.
Hi, im trying to pull something like this, but using a BorderPane instead, being honest i dont know why doesnt work for me. Do you have any tutorial like that?
File>include fxml doesnt work for me as it just doesn't do anything no errors nothing when select open :( any help would be super as im really stressing over this hurdle thanks!
Hi MVP, How can this be applied to making dynamic tabs (meaning, I can't set tab-controller instance variables in MainController since I don't know how many tabs the user is going to create?) (For simplicity's sake, we can assume all tabs are identical in content...) Thanks a lot! (:
Hi +CharvelCX292N, You would have to create a controller that responds to some button click to add new tabs somewhere (basically an action event callback) and have a reference to the your TabPane in order to add new tabs to. It would go something like this (pseudo code) ... FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/tabs/YourTab.fxml")); Tab tab = new Tab("Dynaminc Tab"); tab.setContent(loader.load()); tabPane.getTabs().add(tab); You'll have to figure out where the pieces go in your design but in a nutshell you'll need the above in one form or another. An interesting topic for another tutorial! Thanks for your question.
Last Question my teacher , is it true to say that @FXML void initilize() is the same as void initialize(URL location, ResourceBundle resources) because both they are called before create]ing main FXML page.
Hi +abdullah alsoumahi, If you check the javadocs on the Interface Initializable you will find that this note which I quote below ... "NOTE This interface has been superseded by automatic injection of location and resources properties into the controller. FXMLLoader will now automatically call any suitably annotated no-arg initialize() method defined by the controller. It is recommended that the injection approach be used whenever possible." So the Initialzable Interface has been deprecated in JavaFX 8 (which has the signature initialize(URL location, ResourceBundle resources) ) in favour of a public initialize() method. By the way: You can instead use in your Controller ... @FXML private ResourceBundle resources; @FXML private URL location; and the FXML Loader will inject it for you.
The MVP stands for "Most Valuable Programmer" in Java! It is described in the About link on my RUclips homepage. I can understand the confusion for sure. MVP, MVC etc... I'm afraid there is no cut and dry answer to this. The type of software architecture/design pattern you employ is somewhat related to the type of application you have (Desktop, web ..) and/or the technology stack you are using. You are correct though, I am using MVC in my JavaFX tutorials, its a good fit for Desktop applications.
Oh I see, thx for the comment. I am building a new Desktop Application that plans to be a very big software, I always used MVC Pattern and also heard a lot about MVP and MVVM Patterns, but I saw that a lot people talk about it in theory but very low real projects using it, I think atleast. I do fear to implement MVP now and it give me more trouble in the future, because others developers will work on this and they probably will not follow the same pattern as I have implemented since MVP gives you a little freedom about Presenter and View (SC) e (PV)
All the best with your project. It will surely be a very enriching learning experience. You are clearly very motivated and so, I have no doubt you will succeed.
I have one question why we cannot change or rename the object to any name it is giving me error null value for example @FXML private ConsoleTabController consoleTabController to @FXML private ConsoleTabController renameController why the program restricted ConsoleTabController only to be consoleTabController please any idea about that?
Hi +abdullah alsoumahi Thanks for your question, I'm glad to see your trying things out! How it works =========== The Controller is binded to the .fxml file The convention being that the field name is the value of the fx:id attribute with "Controller" appended. So In your case, the field "consoleTabController" the fx:id="consoleTab" in Main.fxml and then the "Controller" String is appended therefore that is how you end up with "consoleTabController". How to rename ============= It is unfortunately a little painful, in this case go to Main.fxml and change the fx:id to whatever you want, ex: Then go in the MainController Class and rename your field variable to "consoleTab2Controller" ex: @FXML private ConsoleTabController consoleTab2Controller; that should work. Its painful because you have to track down the fx:id in a .fxml file to rename (no IDE help with NetBeans for a one step operation - as far as I know). It pays to come up with good fx:id's from the start and just roll with the convention. Cheers. Andy
Hi, for this tutorial could you provide the source code ? I cannot find it here :) I would like to compare this to the version with SpringBoot to spot the key differences especially when setting up controllers. Thanks!
MVP Java, On any of my other FXML files, the anchor pane fx: iid's are red. It looks like they're never used. Here is a screenshot imgur.com/a/gwaq2. How canI fix this? Because my application doesn't start now
this is so confusing to grasp. better if you just made the whole things in steps and hard coding. I didn't complete it cause I really got confused. trying to follow the structures and methods unfortunately I got lost. lool
+Amro Osman I'm sorry to hear you were confused. Maybe you need to watch it one more time and slow down the speed of the video via the configuration options (I sometimes do this when its the first time I learn something). I indeed did not hard code things, maybe some wouldn't of liked it the other way around, I don't know (hard to please everyone). If you have some questions on the video which I could clear up for you then please don't hesitate. Thanks for you feedback.
This is pure gold. You've saved me countless hours of digging around to accomplish this in my JavaFX app.
Thank you very much!
Great news +Irvel. It took me countless hours myself! Your very welcome.
i have spent at least 8 hours, trying tons of things, going in possibly ALL posts about similar things on google and stackoverflow, the same null and thread errors etc, now IT WORKS !!!!!!!!!!!!!
Awesome! Yes, I've been down the Google results rabbit hole..glad it helped!
More Gotchas that took me 1.5 hours to find out:
- Classes have to have the same name as FXML Files (you can append "Controller" to the name)
- FXML files names have to be apparently capitalized
- Dont remember if he said it but the ID of the FXML top container has to be the same as its name and as the one referenced in the main FXML.
For anyone who is still struggling xD (maybe he said all of that and I am just stupid xD)
Thanks man, this one saved me: "- Classes have to have the same name as FXML Files (you can append "Controller" to the name)"
No that is wrong.
6:53
There is no naming convention there, but in the main controller class when dependency injecting the other controllers:
@FXML ChildController Controller;
i.e. you can mak make fx:id="babana" then the controller variable must be named bananaController
6:53
There is no naming convention there, but in the main controller class when dependency injecting the other controllers:
@FXML ChildController Controller;
i.e. you can mak make fx:id="babana" then the controller variable must be named bananaController
Waiting for you to upload more on javafx. above video was very helpful and the voice was crsytal clear.
This is pure platinum. Thank you so much.
Thanks! Platinum is good :)
What a great tutorial! I finally dove head first in this since my controller class now has 1400 lines and fxml more than 250 lines. It was time to start cleaning that up.
Thank you on your guidance. This is exactly what I needed as I am in the same way working with tabs.
Your welcome Gandeloft, glad it helped out.
Thanks a lot. That was really helpful. Pls make a video on javafx app deployment as single instalable file.
Really nice job! Having to learn JavaFX quickly and this post really helped. Thanks so much!
In my case it does not work because i dont use tabs, i have two controllers and always get nullpoint exception, i done all steps and above
Any reason why you created your controllers manually and not while creating the FXML files? For the latter one, you get the chance to create the controller together with the FXML file (and even a corresponding CSS file, if you want) while walking through the FXML file creation process wizard (at least, in NetBeans it's like that).
The small advantage of this is that the template already adds `implements Initializable` to the class and also adds the method `initialize`...
thank's for your tutorial. I want to know how enable or disable button from another windows form?
+dony kisworo, Sorry for the late reply but RUclips flagged your comment as Spam! Its strange since your comment is just fine. Anyways ... What you need is a reference to the Controller from that other window/Scene (which has that button you want to disable) to be present in whatever code you are trying to do that from. My JavaFX/Spring tutorials show you how to Dependency Inject (DI) a @Controller to allow you to do that.
Thanks for your comment.
And what about data between two Stages when you use Spring Boot ? How do you manage that?
you are my hero
wow, thanks!
great video !!!
Thanks, glad you liked it!
Is it possible to make multiple controllers communicate with each other without using tabs?
awesome demo, just too complicated for a beginner, I am still struggling to get this to work, created a simple project with 1 scene, created 2 fxml files, one for the main and one for the child, the child has a label in it and the main has the child and a button, my challenge to myself is to get the button in the main to change the label in the child, so far .... absolutely no luck. used scene builder as you did
i got error when try to File | Include File. How to solve this issues?
It seems that including fxml file into another fxml doesn't work with border pane :O, but works fine with split pane and archon pane. I use Gluon 10
its kinda weird ... why do we have to reinject the controller if its specified in the fxml the controller in the first place ... have they patched this ? I think this is clearly a bug in java fx
A BIG THANK YOU!!!
Hey, why wouldn't just make the variables static?
hey ..good job man.. keep it up!
thanks Kevin, appreciate it
Hi, thanks for the tutorial.
I've a little problem when i want to write in my textarea with events. (log textArea)
I've a stage with 1 TabPane (with 2 tabs)
in the first tab(LogController), i have a textArea with a button (method createLog), and in the second tab (Tab2controller) i have a simple scene with pane and there's is a button
i'd like write each event cliked into the textArea.
For example, when i click in the button in second tab,the event : javafx.event.ActionEvent[source=Button[id=bttnCreate, styleClass=button]'Create']
I can retrieve by getText method in my LogController
In Tab2Controller:
@FXML
public void toScene1(ActionEvent event) throws IOException {
System.out.println("Bouton Scene 1 cliqué");
FXMLLoader loaderLog = new FXMLLoader();
loaderLog.setLocation(LogTabController.class.getResource("LogTab.fxml"));
loaderLog.load();
LogTabController logTabController = loaderLog.getController();
// logTabController.createLog(event);
// logTabController.monLog(event);
TextArea area = logTabController.getLogTextArea();
// logTabController.setLogTextArea(area);
// area.textProperty().bind(bttnScene1.textProperty());
logTabController.addLog("bob");
in LogController:
@FXML
void createLog(ActionEvent event) {
this.event = event;
logTextArea.setText("Oiiiiii
");
logTextArea.appendText("Ok
");
if (event != null) {
logTextArea.appendText(event.toString() + "
");
}
System.out.println("Loooggg");
System.out.println(event);
}
void addLog(String text) {
logTextArea.clear();
logTextArea.appendText(text + "
");
logTextArea.requestFocus();
logTextArea.setPromptText("qsqsqsqqsqq");
System.out.println(logTextArea.getText());
createLog(event);
}
Thanks for your help
Hi +Surfeur, thanks for your comment. I am not ignoring it but unfortunately, I am too busy to look at your request.. I am very sorry :( I will try to come back to your comment if time permits.
@@MVPJava ok thanks. Can I send you my little source code ?😊😊. I don't know if I must use binding or not.
Man. I just CANT get multiple fxml controllers to work.
Even following this video I still get nullpointer any time I try to change an object (like .setText)
I'm running everything on a single controller now and it's getting really annoying navigating a 2500 line Class file
Nevermind, got it working!!!
Oh man.....it's going to be a nightmare cleaning up this 2500line primary controller.
Hi +Matthew, sorry to hear you had issues. Glad to hear you got it resolved though. If you feel your solution could help others please share it here in the comments.
i have question..can i email you?
Hi, im trying to pull something like this, but using a BorderPane instead, being honest i dont know why doesnt work for me.
Do you have any tutorial like that?
HI Carlos, unfortunately I do not. I might go back to doing some JavaFX tutorials one day but for now I'm afraid that's all I've got.
File>include fxml doesnt work for me as it just doesn't do anything no errors nothing when select open :( any help would be super as im really stressing over this hurdle thanks!
have same problem! did u find anything?
Hi MVP,
How can this be applied to making dynamic tabs (meaning, I can't set tab-controller instance variables in MainController since I don't know how many tabs the user is going to create?)
(For simplicity's sake, we can assume all tabs are identical in content...)
Thanks a lot! (:
Hi +CharvelCX292N,
You would have to create a controller that responds to some button click to add new tabs somewhere (basically an action event callback) and have a reference to the your TabPane in order to add new tabs to. It would go something like this (pseudo code) ...
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/tabs/YourTab.fxml"));
Tab tab = new Tab("Dynaminc Tab");
tab.setContent(loader.load());
tabPane.getTabs().add(tab);
You'll have to figure out where the pieces go in your design but in a nutshell you'll
need the above in one form or another.
An interesting topic for another tutorial!
Thanks for your question.
Last Question my teacher , is it true to say that
@FXML void initilize() is the same as void initialize(URL location, ResourceBundle resources) because both they are called before create]ing main FXML page.
Hi +abdullah alsoumahi,
If you check the javadocs on the Interface Initializable you will find that this note which I quote below ...
"NOTE This interface has been superseded by automatic injection of location and resources properties into the controller. FXMLLoader will now automatically call any suitably annotated
no-arg initialize() method defined by the controller. It is recommended that the injection approach be used whenever possible."
So the Initialzable Interface has been deprecated in JavaFX 8 (which has the signature initialize(URL location, ResourceBundle resources) ) in favour of a public initialize() method. By the way: You can instead use in your Controller ...
@FXML private ResourceBundle resources;
@FXML private URL location;
and the FXML Loader will inject it for you.
@MVP Java, as your name mention "MVP", but you teach here a MVC Pattern. Is what you recommend nowadays?
The MVP stands for "Most Valuable Programmer" in Java! It is described in the About link on my RUclips homepage. I can understand the confusion for sure. MVP, MVC etc... I'm afraid there is no cut and dry answer to this. The type of software architecture/design pattern you employ is somewhat related to the type of application you have (Desktop, web ..) and/or the technology stack you are using. You are correct though, I am using MVC in my JavaFX tutorials, its a good fit for Desktop applications.
Oh I see, thx for the comment. I am building a new Desktop Application that plans to be a very big software, I always used MVC Pattern and also heard a lot about MVP and MVVM Patterns, but I saw that a lot people talk about it in theory but very low real projects using it, I think atleast. I do fear to implement MVP now and it give me more trouble in the future, because others developers will work on this and they probably will not follow the same pattern as I have implemented since MVP gives you a little freedom about Presenter and View (SC) e (PV)
All the best with your project. It will surely be a very enriching learning experience. You are clearly very motivated and so, I have no doubt you will succeed.
I have one question why we cannot change or rename the object to any name it is giving me error null value for example
@FXML private ConsoleTabController consoleTabController to @FXML private ConsoleTabController renameController
why the program restricted ConsoleTabController only to be consoleTabController
please any idea about that?
Hi +abdullah alsoumahi
Thanks for your question, I'm glad to see your trying things out!
How it works
===========
The Controller is binded to the .fxml file
The convention being that the field name is the value of the fx:id attribute with "Controller" appended.
So In your case, the field "consoleTabController"
the fx:id="consoleTab" in Main.fxml
and then the "Controller" String is appended
therefore that is how you end up with "consoleTabController".
How to rename
=============
It is unfortunately a little painful,
in this case go to Main.fxml and change the fx:id to whatever you want,
ex:
Then go in the MainController Class and rename your field variable to "consoleTab2Controller"
ex: @FXML private ConsoleTabController consoleTab2Controller;
that should work.
Its painful because you have to track down the fx:id in a .fxml file to rename
(no IDE help with NetBeans for a one step operation - as far as I know).
It pays to come up with good fx:id's from the start and just roll with the convention.
Cheers.
Andy
Thaaaank you for your clear answer , now I understand . hope all the best to you and continue explaining to us.
Thanks. This clarification helped me as well.
U helper another one, xd. If only it was written in some place... Thanks man! You the Best!
Do you have a github for this?
no but usually the description holds a link to a zip file where you can get the code.
Hi, for this tutorial could you provide the source code ? I cannot find it here :) I would like to compare this to the version with SpringBoot to spot the key differences especially when setting up controllers. Thanks!
Well I don't know how that escaped me but you are right, the link was not there. I just updated the description section with the link. Enjoy.
MVP Java, On any of my other FXML files, the anchor pane fx: iid's are red. It looks like they're never used. Here is a screenshot imgur.com/a/gwaq2. How canI fix this? Because my application doesn't start now
this is so confusing to grasp. better if you just made the whole things in steps and hard coding. I didn't complete it cause I really got confused. trying to follow the structures and methods unfortunately I got lost. lool
+Amro Osman I'm sorry to hear you were confused. Maybe you need to watch it one more time and slow down the speed of the video via the configuration options (I sometimes do this when its the first time I learn something). I indeed did not hard code things, maybe some wouldn't of liked it the other way around, I don't know (hard to please everyone).
If you have some questions on the video which I could clear up for you then please don't hesitate.
Thanks for you feedback.
MVP Java
thanks bro. I have no question for now but after I complete the tutorial in slow mode may be I would. thanks any way.