控制器之间的通信JavaFX

时间:2016-06-12 17:14:52

标签: javafx controller communication scene

当我点击主舞台的矩形时,我试图在新窗口上显示图像但是我收到以下错误:“线程中的异常”JavaFX应用程序线程“java.lang.NullPointerException”

第一个控制器(ControllerImpl)初始化第二个控制器(ControllerImage)并调用其中一个方法(controllerImage.displayImageSel):

    @FXML
private ControllerImage controllerImage = new ControllerImage();
public void rectangleSave(Rectangle r, String imagePath) {
    r.setOnMousePressed((event) -> {
        try {
            Stage imageStage = new Stage();
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/images.fxml"));

            Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
            imageStage.setX(0);
            imageStage.setY(0);
            imageStage.setHeight(primScreenBounds.getHeight());
            imageStage.setWidth(primScreenBounds.getWidth() * 0.7);
            imageStage.setTitle("JavaFX Scene Graph Demo");

            Scene scene = new Scene(loader.load());
            imageStage.setScene(scene);
            imageStage.show();

            controllerImage.displayImageSel(imagePath);

        } catch (IOException e) {
            System.out.println("Me cago en el PP");
        }
    });
}

第二个控制器包含一个由ControllerImpl调用的堆栈窗格和方法:

@FXML
public StackPane Spi;
public void displayImageSel(String imagePath) {
    Rectangle ri = new Rectangle();
    Spi.getChildren().add(ri);
    Image image = new Image(new File(imagePath).toURI().toString());
    ri.setFill(new ImagePattern(image));
}

当然Spi在FXML文件中定义:

       <StackPane fx:id="Spi"......

问题是ControllerImage无法找到StackPane Spi。我一直在实施与此相关的各种解决方案但到目前为止还没有人工作过。

感谢您的帮助!

编辑:新错误消息

javafx.fxml.LoadException: Root value already specified.

/C:/tutorial-app/tutorial-app/target/resources/main/images.fxml /C:/tutorial-app/tutorial-app/target/resources/main/images.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2755)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2704)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at tutorial.controller.impl.ControllerImpl.lambda$rectangleSave$1(ControllerImpl.java:146)
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.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$MouseHandler.process(Scene.java:3757)
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:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
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)

1 个答案:

答案 0 :(得分:0)

您自己创建controllerImage,而不是FXMLLoader,因此FXML字段永远不会被注入。 你也永远不会在那个加载器上调用load 编辑:糟糕,你这样做,我只是错过了它。

假设您在“images.fxml”中正确设置了fx:controller,请删除controllerImage的分配,并在调用FXMLLoader#load后添加此行:

controllerImage = loader.getController();

如果您没有在FXML中设置fx:controller,您可以将其设置为正确的类,或者将您创建的实例传递给加载器 加载之前: p>

loader.setController(controllerImage);

在这种情况下,您当然会保留controllerImage的初始化。