Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8101740

Automatically inject "location" and "resources" fields in FXML controller

XMLWordPrintable

      FXML controllers can currently request initialization notification by implementing the Initializable interface. However, there are several drawbacks to this approach:

      1) Because it is an interface, it is difficult to modify or extend the behavior of Initializable without breaking backwards compatibility.

      2) Also because it is an interface, implementations of Initializable's initialize() method must be public. However, this method is really only meant to be called by FXMLLoader, so it should ideally be protected or private to the implementing class.

      Instead, FXMLLoader should automatically inject the location and resources values that are currently passed to initialize() into suitably annotated fields in the controller; e.g.:

      @FXML private ResourceBundle resources;

      FXMLLoader should also call a suitably annotated no-arg initialization method, if provided; e.g.:

      @FXML
      protected void initialize() {
         // ...
      }

      This approach is more consistent with how other aspects of the controller are handled and is easier to extend in the future without breaking backwards compatibility. The Initializable interface should be deprecated in favor of this approach.

      Note that this change supersedes the earlier proposal to add an abstract base Controller class. Such a class is no longer needed.

            gkbrown Greg Brown (Inactive)
            gkbrown Greg Brown (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: