JavaFX Multiple Controllers

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ธ.ค. 2024

ความคิดเห็น • 70

  • @doa_form
    @doa_form 8 ปีที่แล้ว +16

    This is pure gold. You've saved me countless hours of digging around to accomplish this in my JavaFX app.
    Thank you very much!

    • @MVPJava
      @MVPJava  8 ปีที่แล้ว +2

      Great news +Irvel. It took me countless hours myself! Your very welcome.

  • @shoyur
    @shoyur 2 ปีที่แล้ว +1

    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 !!!!!!!!!!!!!

    • @MVPJava
      @MVPJava  2 ปีที่แล้ว

      Awesome! Yes, I've been down the Google results rabbit hole..glad it helped!

  • @truth-7380
    @truth-7380 ปีที่แล้ว +1

    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

  • @EverlastingSky
    @EverlastingSky 5 ปีที่แล้ว +1

    This is pure platinum. Thank you so much.

    • @MVPJava
      @MVPJava  5 ปีที่แล้ว

      Thanks! Platinum is good :)

  • @mohittaparia3910
    @mohittaparia3910 8 ปีที่แล้ว +5

    Waiting for you to upload more on javafx. above video was very helpful and the voice was crsytal clear.

  • @trashtatur
    @trashtatur 5 ปีที่แล้ว +3

    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)

    • @MajinHico
      @MajinHico 4 ปีที่แล้ว

      Thanks man, this one saved me: "- Classes have to have the same name as FXML Files (you can append "Controller" to the name)"

    • @truth-7380
      @truth-7380 ปีที่แล้ว +1

      No that is wrong.

    • @truth-7380
      @truth-7380 ปีที่แล้ว +1

      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

  • @Gandeloft
    @Gandeloft 3 ปีที่แล้ว

    Thank you on your guidance. This is exactly what I needed as I am in the same way working with tabs.

    • @MVPJava
      @MVPJava  3 ปีที่แล้ว

      Your welcome Gandeloft, glad it helped out.

  • @RamHomier
    @RamHomier 6 ปีที่แล้ว

    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.

  • @IRgEEK
    @IRgEEK 7 ปีที่แล้ว

    Really nice job! Having to learn JavaFX quickly and this post really helped. Thanks so much!

  • @lionson5253
    @lionson5253 4 ปีที่แล้ว

    you are my hero

    • @MVPJava
      @MVPJava  4 ปีที่แล้ว

      wow, thanks!

  • @learnsomething6730
    @learnsomething6730 2 ปีที่แล้ว +1

    Thanks a lot. That was really helpful. Pls make a video on javafx app deployment as single instalable file.

  • @randomnet8149
    @randomnet8149 7 ปีที่แล้ว +1

    A BIG THANK YOU!!!

  • @kevinflorenzdaus
    @kevinflorenzdaus 7 ปีที่แล้ว

    hey ..good job man.. keep it up!

    • @MVPJava
      @MVPJava  7 ปีที่แล้ว

      thanks Kevin, appreciate it

  • @tr233
    @tr233 5 ปีที่แล้ว +1

    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

  • @michaelsd28
    @michaelsd28 3 ปีที่แล้ว

    great video !!!

    • @MVPJava
      @MVPJava  3 ปีที่แล้ว

      Thanks, glad you liked it!

  • @geertvancompernolle2586
    @geertvancompernolle2586 6 ปีที่แล้ว

    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`...

  • @donykisworo6709
    @donykisworo6709 8 ปีที่แล้ว +1

    thank's for your tutorial. I want to know how enable or disable button from another windows form?

    • @MVPJava
      @MVPJava  8 ปีที่แล้ว

      +dony kisworo, Sorry for the late reply but TH-cam 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.

  • @TheSajdovski
    @TheSajdovski 7 ปีที่แล้ว

    And what about data between two Stages when you use Spring Boot ? How do you manage that?

  • @yonishachar1887
    @yonishachar1887 5 ปีที่แล้ว

    Hey, why wouldn't just make the variables static?

  • @mioszblasiak4517
    @mioszblasiak4517 6 ปีที่แล้ว

    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

  • @ziyadkhalil8592
    @ziyadkhalil8592 6 ปีที่แล้ว

    Is it possible to make multiple controllers communicate with each other without using tabs?

  • @m.ataide
    @m.ataide 8 ปีที่แล้ว

    @MVP Java, as your name mention "MVP", but you teach here a MVC Pattern. Is what you recommend nowadays?

    • @MVPJava
      @MVPJava  8 ปีที่แล้ว

      The MVP stands for "Most Valuable Programmer" in Java! It is described in the About link on my TH-cam 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.

    • @m.ataide
      @m.ataide 8 ปีที่แล้ว

      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)

    • @MVPJava
      @MVPJava  8 ปีที่แล้ว

      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.

  • @surfeurdestemps3193
    @surfeurdestemps3193 3 ปีที่แล้ว

    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

    • @MVPJava
      @MVPJava  3 ปีที่แล้ว +1

      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.

    • @surfeurdestemps3193
      @surfeurdestemps3193 3 ปีที่แล้ว

      @@MVPJava ok thanks. Can I send you my little source code ?😊😊. I don't know if I must use binding or not.

  • @frederikstephano2926
    @frederikstephano2926 6 ปีที่แล้ว

    i got error when try to File | Include File. How to solve this issues?

  • @verrymariyanto8377
    @verrymariyanto8377 6 ปีที่แล้ว

    i have question..can i email you?

  • @CharvelCX292N
    @CharvelCX292N 8 ปีที่แล้ว

    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! (:

    • @MVPJava
      @MVPJava  8 ปีที่แล้ว +1

      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.

  • @maxjustmax521
    @maxjustmax521 3 ปีที่แล้ว

    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

  • @hellsing357
    @hellsing357 7 ปีที่แล้ว

    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

  • @carlosaxelreyesamezcua8557
    @carlosaxelreyesamezcua8557 5 ปีที่แล้ว

    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?

    • @MVPJava
      @MVPJava  5 ปีที่แล้ว

      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.

  • @abdullahalsoumahi8423
    @abdullahalsoumahi8423 7 ปีที่แล้ว

    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.

    • @MVPJava
      @MVPJava  7 ปีที่แล้ว +1

      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.

  • @Mohrenberg
    @Mohrenberg 4 ปีที่แล้ว

    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

    • @Mohrenberg
      @Mohrenberg 4 ปีที่แล้ว

      Nevermind, got it working!!!
      Oh man.....it's going to be a nightmare cleaning up this 2500line primary controller.

    • @MVPJava
      @MVPJava  4 ปีที่แล้ว +1

      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.

  • @seanbarrish8398
    @seanbarrish8398 7 ปีที่แล้ว

    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!

    • @VienneseGamer
      @VienneseGamer 6 ปีที่แล้ว

      have same problem! did u find anything?

  • @abdullahalsoumahi8423
    @abdullahalsoumahi8423 7 ปีที่แล้ว

    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?

    • @MVPJava
      @MVPJava  7 ปีที่แล้ว

      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

    • @abdullahalsoumahi8423
      @abdullahalsoumahi8423 7 ปีที่แล้ว +1

      Thaaaank you for your clear answer , now I understand . hope all the best to you and continue explaining to us.

    • @tricksterismeaningless9095
      @tricksterismeaningless9095 7 ปีที่แล้ว

      Thanks. This clarification helped me as well.

    • @Yvtq8K3n
      @Yvtq8K3n 6 ปีที่แล้ว

      U helper another one, xd. If only it was written in some place... Thanks man! You the Best!

  • @eldrake3714
    @eldrake3714 8 ปีที่แล้ว

    Do you have a github for this?

    • @MVPJava
      @MVPJava  8 ปีที่แล้ว +1

      no but usually the description holds a link to a zip file where you can get the code.

    • @zabuldog
      @zabuldog 8 ปีที่แล้ว

      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!

    • @MVPJava
      @MVPJava  8 ปีที่แล้ว +1

      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.

  • @Feudelcosine148
    @Feudelcosine148 7 ปีที่แล้ว

    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

  • @3mro_coding
    @3mro_coding 7 ปีที่แล้ว

    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

    • @MVPJava
      @MVPJava  7 ปีที่แล้ว +1

      +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.

    • @3mro_coding
      @3mro_coding 7 ปีที่แล้ว +1

      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.