1

I have 6 fxml files having one controller . I need initialize method not on first fxml but on 5th fxml file(PasswordArray.fxml) for customization of buttons. Since initialize() method is called automatically on load of first fxml file(Home.fxml) , it shows an error. How to use now initialize method after PasswordArray.fxml . I searched on internet but do not found any solution of my question. Should I use diffferent controllers , because I need initialize method for more than one fxml file.Main FXML File (Home.fxml)

Controller (MajorProjectController.java) [initialize() method][3]

  • 1
    *"I have 6 fxml files having one controller"*. First: you don't. You get a new controller every time you load an FXML file. What you probably mean is *"I have 6 fxml files, and the controllers for all of them are from the same class"*. Just **don't do this**. Create a different controller class for each FXML. – James_D Apr 07 '17 at 13:54
  • Thanks #James_D for your suggestion. But I have a query "I don't need other custom operations for all fxml files but just some of the files need initialize method to do some extra operations on GUI. So should I take different controllers for all of my fxml files or for just those files who need initialize method to perform some customization ? " – Mohita Srivastava Apr 07 '17 at 15:42
  • You should basically **never** use the same controller class for different FXML files. The only possible exception to that (that I can think of) would be if you had two FXML files that had *exactly* the same set of controls, but just laid them out differently (e.g. if you were writing for both desktop and mobile and had different layouts: but it's unlikely you could make that work for a variety of reasons anyway). – James_D Apr 07 '17 at 15:50
  • Okay but how can we make one more controller ? Since I am new to javafx and fxml, what I know is "when we make new 'JavaFX FXML Application' in netbeans , it automatically made a controller java file. " How can I now make a new controller file? – Mohita Srivastava Apr 07 '17 at 15:57
  • Just make a new class. I don't really understand what you are asking. – James_D Apr 07 '17 at 16:11

1 Answers1

0

You can check the file name in the URL location parameter of the initialize method and trigger your actions accordingly. Note that, FXMLLoader will create a separate instance of the controller class for each individual FXML - unless you provide your own manually to the FXMLLoader instance.

Zsolt
  • 145
  • 5
  • But I am using initialize() method with no parameters. Because when I use it with parameters , I am not able to call it from different @FXML annotated method. What Should I do now ? – Mohita Srivastava Apr 07 '17 at 15:44
  • @MohitaSrivastava If you are calling `initialize()` from other methods in the controller, you are basically doing things wrong. The `initialize()` method is for initialization, i.e. it should only be called once per controller instance. – James_D Apr 07 '17 at 15:51
  • 1
    Also for using the no-argument `initialize` method you can still add a field named `location`, make it visible to the `FXMLLoader` and `FXMLLoader` will inject the location url to this field. Note that I'd consider checking the location in the `initialize` method bad since the url will not be obvious and a fxml file may be renamed breaking the code... – fabian Apr 07 '17 at 17:59