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

前端 未结 1 1564
日久生厌
日久生厌 2020-12-19 08:01

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

InaccessibleObjectException: Unable to make field private javafx.scene.control.Button tech.fl         


        
相关标签:
1条回答
  • 2020-12-19 08:46

    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.

    0 讨论(0)
提交回复
热议问题