Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct to make FXML members in Java 9 or 10?

After upgrading to Java 10 (from 8), I'm getting these errors:

InaccessibleObjectException: Unable to make field private javafx.scene.control.Button tech.flexpoint.dashman.controllers.configurator.RegistrationController.registerButton accessible: module tech.flexpoint.dashman does not "opens tech.flexpoint.dashman.controllers.configurator" to module javafx.fxml

Does it mean I should make them public? Does that make the @FXML annotation essentially useless in Java 9 and 10?

like image 348
pupeno Avatar asked Jun 28 '18 12:06

pupeno


People also ask

Is FXML annotation required?

The @FXML annotation is required for the controller class's private member fields; otherwise, field injection would fail.

What is FXML file in Java?

FXML is an XML-based user interface markup language created by Oracle Corporation for defining the user interface of a JavaFX application. FXML presents an alternative to designing user interfaces using procedural code, and allows for abstracting program design from program logic.

What is the full form of FXML?

FX Markup Language. In the same way that HTML is for Hypertext Markup Language, and many more acronyms that end in ML .


1 Answers

Since you're using a module, reflection is not allowed to access private members of your classes by default. The exception basically tells you what needs to be done:

module tech.flexpoint.dashman {
    ...

    // allow everyone to access classes in tech.flexpoint.dashman.controllers.configurator via reflection
    opens tech.flexpoint.dashman.controllers.configurator;
}

or

module tech.flexpoint.dashman {
    ...

    // allow only module javafx.fxml access classes in tech.flexpoint.dashman.controllers.configurator via reflection
    opens tech.flexpoint.dashman.controllers.configurator to javafx.fxml;
}

This does not make @FXML useless. It's still needed to mark non-public members that FXMLLoader is allowed to use, it's just required to explicitly state that reflection is allowed to override access to members. (FXMLLoader uses reflection so at least the javafx.fxml module needs this kind of access for injection to work.)

Depending on the contents of your package it could be beneficial to move the controller(s) to it's own subpackage to not allow reflective access to non-controller classes.

like image 84
fabian Avatar answered Sep 21 '22 23:09

fabian