如何使用fxml文件和流库获取控制器中的参数?

时间:2016-09-29 15:06:26

标签: java javafx

如何在构造函数中使用参数“Settings”对象调用MoviesSettingsController,其中Settings已在MainController中进行实例化和初始化?我必须在SideMenuController然后在MoviesSettingsController传递设置?我对此感到失望。 您将在

之后找到我的所有文件

目前,我收到了这个错误:

io.datafx.controller.flow.FlowException: io.datafx.controller.FxmlLoadException: java.lang.InstantiationException: maxiRename.controller.MoviesSettingsController
    at io.datafx.controller.flow.action.FlowLink.handle(FlowLink.java:72)
    at io.datafx.controller.flow.FlowHandler.handle(FlowHandler.java:231)
    at io.datafx.controller.flow.FlowHandler.handle(FlowHandler.java:163)
    at maxiRename.controller.SideMenuController.lambda$0(SideMenuController.java:79)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at com.jfoenix.controls.JFXListCell.lambda$updateItem$157(JFXListCell.java:295)
    at com.jfoenix.controls.JFXListCell.access$lambda$9(JFXListCell.java)
    at com.jfoenix.controls.JFXListCell$$Lambda$10.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: io.datafx.controller.FxmlLoadException: java.lang.InstantiationException: maxiRename.controller.MoviesSettingsController
    at io.datafx.controller.ViewFactory.createByController(ViewFactory.java:180)
    at io.datafx.controller.flow.action.FlowLink.handle(FlowLink.java:69)
    ... 85 more
Caused by: java.lang.InstantiationException: maxiRename.controller.MoviesSettingsController
    at java.lang.Class.newInstance(Class.java:427)
    at io.datafx.controller.ViewFactory.createByController(ViewFactory.java:148)
    ... 86 more
Caused by: java.lang.NoSuchMethodException: maxiRename.controller.MoviesSettingsController.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 87 more

MainApp.java:

public void start(Stage stage) throws Exception {
    Flow flow = new Flow(MainController.class);
    DefaultFlowContainer container = new DefaultFlowContainer();
    flowContext = new ViewFlowContext();
    flowContext.register("Stage", stage);
    flow.createHandler(flowContext).start(container);

    JFXDecorator decorator = new JFXDecorator(stage, container.getView());
    decorator.setCustomMaximize(true);
    Scene scene = new Scene(decorator, 770, 690);
    scene.getStylesheets().add(MainMaxiFilm.class.getResource("/resources/css/jfoenix-main-demo.css").toExternalForm());
    stage.setWidth(770);
    stage.setHeight(690);
    stage.setResizable(false);
    stage.setScene(scene);
    stage.show();
}

MainController.java:

@FXMLController(value = "/resources/fxml/Main.fxml", title = "Material Design Example")
public class MainController implements Initializable{

    @FXMLViewFlowContext
    private ViewFlowContext context;
    @FXML 
    private StackPane root;

    @FXML
    private BorderPane mainBorderPane;

    @FXML
    private VBox mainVBoxTop;

    @FXML
    private JFXToolbar mainToolbarTop;

    @FXML
    private JFXHamburger mainHamburgerTop;

    @FXML
    private JFXDrawer mainDrawer;

    private FlowHandler flowHandler;
    private FlowHandler sideMenuFlowHandler;

    public void initialize(URL url, ResourceBundle rb) {
        try {
            HamburgerBackArrowBasicTransition transition = new HamburgerBackArrowBasicTransition(mainHamburgerTop);
            transition.setRate(-1);

            mainDrawer.setOnDrawerOpening((e) -> {
                transition.setRate(1);
                transition.play();
            });

            mainDrawer.setOnDrawerClosing((e) -> {
                transition.setRate(-1);
                transition.play();
            });

            mainHamburgerTop.setOnMouseClicked((e)->{
                if (mainDrawer. isShown() || mainDrawer.isShowing()) {
                    mainDrawer.close();
                }
                else 
                    mainDrawer.open();
            });

            // create the inner flow and content
            context = new ViewFlowContext();
            // set the default controller 
            Flow innerFlow = new Flow(HomeController.class);

            flowHandler = innerFlow.createHandler(context);
            context.register("ContentFlowHandler", flowHandler);
            context.register("ContentFlow", innerFlow);
            mainDrawer.setContent(flowHandler.start());
            context.register("ContentPane", mainDrawer.getContent().get(0));

            // side controller will add links to the content flow
            Flow sideMenuFlow = new Flow(SideMenuController.class);
            sideMenuFlowHandler = sideMenuFlow.createHandler(context);
            mainDrawer.setSidePane(sideMenuFlowHandler.start());
        } catch (FlowException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }  
}

SideMenuController.java:

@FXMLController(value = "/resources/fxml/SideMenu.fxml", title = "Material Design Example")
public class SideMenuController {


    @FXML
    private StackPane root;

    @FXML
    private VBox listViewDrawer;

    @FXML
    private ImageView imageTopDrawer;

    @FXML
    private JFXListView<?> sideList;

    @FXML
    private Label home;

    @FXML
    private JFXListView<?> subList;

    @FXML
    private Label musics;

    @FXML
    private Label movies;

    @FXML
    private Label series;

    @FXML
    private Label help;

    @FXML
    private Label exit;

    @FXMLViewFlowContext
    private ViewFlowContext context;

    @PostConstruct
    public void init() throws FlowException, VetoException {
        sideList.propagateMouseEventsToParent();
        System.out.println(context);
        FlowHandler contentFlowHandler = (FlowHandler) context.getRegisteredObject("ContentFlowHandler");
        Flow contentFlow = (Flow) context.getRegisteredObject("ContentFlow");
        System.out.println(subList.getItems().get(0));
        bindNodeToController(home, HomeController.class, contentFlow, contentFlowHandler);
        bindNodeToController(musics, MusicsSettingsController.class, contentFlow, contentFlowHandler);
        bindNodeToController(movies, MoviesSettingsController.class, contentFlow, contentFlowHandler);
        bindNodeToController(series, SeriesSettingsController.class, contentFlow, contentFlowHandler);
        bindNodeToController(help, HelpController.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();
            }
        });
    }
}

和MoviesSettingsControler.java:

@FXMLController(value = "/resources/fxml/MoviesSettings.fxml" , title = "Material Design Example")
public class MoviesSettingsController{

    @FXML
    private GridPane gridSettingsMovies;

    @FXML
    private Label labelSettingsMovies;

    @FXML
    private Label labelFilesNameSettingsMovies;

    @FXML
    private JFXButton buttonCancelMoviesSettings;

    @FXML
    private JFXButton buttonOKMoviesSettings;

    @FXML
    private GridPane gridFilesName;

    @FXML
    private ToggleGroup groupFilesName;

    @FXML
    private JFXRadioButton radioButtonFiles5;

    @FXML
    private JFXRadioButton radioButtonFiles6;

    @FXML
    private JFXRadioButton radioButtonFiles3;

    @FXML
    private JFXRadioButton radioButtonFiles2;

    @FXML
    private JFXRadioButton radioButtonFiles1;

    @FXML
    private JFXRadioButton radioButtonFiles4;

    private Settings settings = new Settings();

    public MoviesSettingsController (Settings pSettings)
    {
        this.settings = pSettings;
    }

    public Settings getSettings() {
        return settings;
    }

    public void setSettings(Settings settings) {
        this.settings = settings;
    }

    public void saveChange (int pRb, int pPart)
    {
        this.settings.setMovieName(pRb);    
    }

    @FXML
    void saveSettingsMovies(ActionEvent event) {
        System.out.println(groupFilesName.getSelectedToggle());
    }

我想要控制器中的Settings对象。 问题在于此(我认为)(代码来自SideMenuController.java):

@PostConstruct
    public void init() throws FlowException, VetoException {
        sideList.propagateMouseEventsToParent();
        System.out.println(context);
        FlowHandler contentFlowHandler = (FlowHandler) context.getRegisteredObject("ContentFlowHandler");
        Flow contentFlow = (Flow) context.getRegisteredObject("ContentFlow");
        System.out.println(subList.getItems().get(0));
        bindNodeToController(home, HomeController.class, contentFlow, contentFlowHandler);
        bindNodeToController(musics, MusicsSettingsController.class, contentFlow, contentFlowHandler);
        bindNodeToController(movies, MoviesSettingsController.class, contentFlow, contentFlowHandler);
        bindNodeToController(series, SeriesSettingsController.class, contentFlow, contentFlowHandler);
        bindNodeToController(help, HelpController.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();
            }
        });
    }

非常感谢:)

1 个答案:

答案 0 :(得分:3)

好的,我找到了有用的东西。如果你看到更好的方法,请告诉我:)。

我在MainController中创建了这样的上下文:

ViewFlowContext context = new ViewFlowContext();

我在其中填充了我希望通过Flow对象传递给其他组件的所有参数和内容。我这样填写:

@FXML
private JFXDrawer mainDrawer;
private FlowHandler flowHandler;
private FlowHandler sideMenuFlowHandler;

context.register("ContentFlowHandler", flowHandler);
context.register("Settings", new Settings());
context.register("ContentFlow", innerFlow);

我正在使用Flow库。我将所有变量和对象传递给上下文:

Flow sideMenuFlow = new Flow(SideMenuController.class);
sideMenuFlowHandler = sideMenuFlow.createHandler(context);
mainDrawer.setSidePane(sideMenuFlowHandler.start()); 

之后进入sideMenuController。

context中取回所有这些内容:

FlowHandler contentFlowHandler = (FlowHandler) context.getRegisteredObject("ContentFlowHandler");
Flow contentFlow = (Flow) context.getRegisteredObject("ContentFlow");

要完成,在movieSettingsController中,我通过上下文获取值,并且它是:)

settings = (Settings) context.getRegisteredObject("settings");

最后一件事,当您想要使用它时,不要忘记控制器中的注释@PostConstruct。 例如:

@PostConstruct
public void init() {
    // TODO Auto-generated method stub
    settings = (Settings) context.getRegisteredObject("Settings");
}

我希望这对其他人有用。当你开始发现这个世界时,这并不容易。