JavaFX: can controller be an abstract class?

吃可爱长大的小学妹 提交于 2019-12-11 02:59:54

问题


I have been working on this part of code for a day and just can't figure out why it always generates error.

I have a controller and FXML. They worked perfectly. Then it came to my mind that I want to reuse this particular controller with a abstract updateSelect() function. Therefore, I change the controller to abstract.

The code compiled just fine. Until I try to run this part of code.

@FXML
private void mnuProjMember_onClick(ActionEvent event) {
    mainContent.getChildren().clear();
    FXMLLoader loader = new      FXMLLoader(getClass().getResource("PaneProjectSearch.fxml"));

    PaneProjectSearchController controller = new PaneProjectSearchController(){
        @Override
        void updateSelect(){
            System.out.println("update: !!");
        }
    };

    loader.setController(controller);
    controller.setParent(mainContent);
    fitToParent(loader);
}

It gives me following error message. Well...it's non-sense because the code will work fine again after I remove the abstract parts without even touching the FXML or other functions.

Error resolving onAction='#btnAdd_onClick', either the event handler is not in the Namespace or there is an error in the script. file:/D:/NetBeansWork/ProjCostTracking/dist/run1210215635/ProjCostTracking.jar!/ProjCostTracking/PaneProjectSearch.fxml:20

Any guidance and advise is welcomed, thanks :)


回答1:


I'm guessing that you have a private handler method in the abstract controller class. To make this work, I think the handler methods, as well as any @FXML-annotated fields, need to be directly accessible by the subclass (i.e. public or protected, or default visibility if the subclass is in the same package as the abstract controller).



来源:https://stackoverflow.com/questions/22832599/javafx-can-controller-be-an-abstract-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!