1

我想为我的客户创建一个 JavaFX 应用程序。我找到了 DataFX 框架并喜欢它。但是我有问题,@FXMLViewFlowContext因为null每次我从父母那里调用 child fview 时它都会返回。我使用了 lib 版本 8.0.7 及以下代码

  1. 我使用 i18n 资源在主控制器中创建侧视图:

    // side controller will add links to the content flow
    Flow sideMenuFlow = new Flow(SideMenuController.class);
    sideMenuFlowHandler = new FlowHandler(sideMenuFlow, context, Common.getViewConfiguration());
    drawer.setSidePane(sideMenuFlowHandler.start(new AnimatedFlowContainer(Duration.millis(320), ContainerAnimations.SWIPE_LEFT)));
    
  2. 这是我的侧窗格控制器

    @FXMLController(value = "/res/fxml/test.fxml")
    public class SideMenuController {
        @FXMLViewFlowContext
        private ViewFlowContext context;
    
    
        @FXML
        private JFXListView test;
    
    
        @FXML
        @ActionTrigger("suppliers")
        private JFXButton suppliers;
    
    
        @FXML
        @ActionTrigger("history")
        private JFXButton history;
    
    
        @FXML
        @ActionTrigger("barcode")
        private JFXButton barcode;
    
    
        @FXML
        @ActionTrigger("settings")
        private JFXButton settings;
    
    
        @FXML
        @ActionTrigger("report")
        private JFXButton report;
    
    
        @FXML
        @ActionTrigger("users")
        private JFXButton users;
    
    
        @FXML
        @ActionTrigger("inventory")
        private JFXButton inventory;
    
    
        @FXML
        @ActionTrigger("shops")
        private JFXButton shops;
    
    
        @FXML
        @ActionTrigger("calculation")
        private JFXButton calculation;
    
    
        @PostConstruct
        public void initialize(){
            test.propagateMouseEventsToParent();
            System.out.println(context);
    
    
            FlowHandler contentFlowHandler = (FlowHandler) context.getRegisteredObject("ContentFlowHandler");
            Flow contentFlow = (Flow) context.getRegisteredObject("ContentFlow");
            bindNodeToController(suppliers, SuppliersController.class, contentFlow, contentFlowHandler);
            bindNodeToController(inventory, InventoryController.class, contentFlow, contentFlowHandler);
            bindNodeToController(users, UsersController.class, contentFlow, contentFlowHandler);
            bindNodeToController(history, HistoryController.class, contentFlow, contentFlowHandler);
            bindNodeToController(shops, ShopsController.class, contentFlow, contentFlowHandler);
            bindNodeToController(report, ReportController.class, contentFlow, contentFlowHandler);
            bindNodeToController(barcode, BarcodeController.class, contentFlow, contentFlowHandler);
            bindNodeToController(calculation, CalculationController.class, contentFlow, contentFlowHandler);
            bindNodeToController(settings, SettingsController.class, contentFlow, contentFlowHandler);
        }
    
    
        private void bindNodeToController(Node node, Class<?> controllerClass, Flow flow, FlowHandler flowHandler) {
            flow.withGlobalLink(node.getId(), controllerClass);
            node.setOnMouseClicked((e) -> {
                try {
                    flowHandler.handle(node.getId());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
        }
    }
    

当我运行我的程序时,我得到一个NullPointerException因为上下文从未被注入。我搜索但没有找到任何解决方案。我也尝试过旧图书馆,但没有帮助。

4

1 回答 1

2

最后我解决了我的问题。这个问题有两个原因。1.第一个与DataFX库版本有关。2.第二个问题是初始化方法。当DataFX开始创建控制器类实例时, javafx FXMLLoader.load()方法将调用初始化方法,这发生在 init ViewFlowContext之前,所以我得到了NullPointerException。换句话说,如果您使用@PostConstruct注释并使用名称initialize调用您的方法,就像我一样,您的方法将被调用两次,一次是在创建新的控制器类实例时(上下文尚未初始化),另一次是在@PostConstruct方法将被调用(上下文已创建)。这适用于DataFX 流库 8.0b8版本。我检查了这个版本的lib 8.0.7但我再次得到NullPointerException。所以我改变了我的lib版本,现在一切正常

于 2016-11-13T12:48:51.067 回答