1

What is the purpose of URL & ResourceBundle in the following code

public class HelloWorld implements Initializable  {

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        ...
    }
}
Kolya Kliment
  • 25
  • 1
  • 4

1 Answers1

5

As per the documentation for the Initializable interface:

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.

You should use something like this instead:

public class Controller
{
    @FXML
    private URL location;
    @FXML
    private ResourceBundle resources;

    public void initialize()
    {
      // do your setup stuff here
      // fxml loader will call this for you
    }
}

Additional note: The above quote calls for a 'suitably annotated no-arg initialize() method'. If for whatever reason you require a private initialization, make sure you 'suitably annotate' it with the @FXML annotation.

Eric
  • 601
  • 7
  • 22
  • can you please give an example of private URL location; & private ResourceBundle resources; usage – Kolya Kliment May 23 '17 at 06:04
  • 1. 'location' is the location of the .fxml file. 2. 'resources' can be used to use strings of different languages: see https://stackoverflow.com/questions/26325403/how-to-implement-language-support-for-javafx-in-fxml-documents – Eric May 23 '17 at 13:15