-1

I've been endlessly struggling with this code, I'm trying to send the values of 2 strings to another "window" to perform a function on them but I keep getting all sorts of errors.

I've thoroughly read the Dependancy Injection thread and the main problem with it was with the (Pane) cast in the 10th line.

Also, I've read this one as well and the answer and the code is just plain chaotic.

What I am really trying to do is this:

I have lets say Main document and I'm trying to send one of its values to its CHILD:

My code is like this for the MAINcontroller:

@FXML
void initialize() throws MalformedURLException
{
   parameterSender.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent event)
        {
            Parent root;
            try
            {
                root = FXMLLoader.load(getClass().getClassLoader().getResource("filetree/childWindow.fxml"),resources);
                Stage stage = new Stage();
                stage.setTitle("Child");
                stage.setScene(new Scene(root, 500, 500));
                FXMLLoader loader = FXMLLoader(getClass().getClassLoader().getResource("filetree/childWindow.fxml"), resources); //I fixed this, thank you very much, Uchiha Itachi
                childWindowController controller = loader.<childWindowController>getController();
                controller.firstParameter = getFirstField(); //this is the getter of the firstTextAreaField
                controller.secondParameter = getSecondField();//this is the getter of the secondTextAreaField
                stage.show();
                } catch (IOException e)
            {
                e.printStackTrace();
            }//End of Catch
}//End of Initialize

And for the CHILDcontroller:

@FXML
void testAcquiredParametersFromParent(MouseEvent event)
{
    System.out.println(firstParameter + " " + secondParameter);
}

When I run this code I get this exception on runtime:

java.lang.ClassCastException: javafx.scene.layout.AnchorPane cannot be cast to javafx.fxml.FXMLLoader
at filetree.FXMLDocumentController$5.handle(FXMLDocumentController.java:17)
at filetree.FXMLDocumentController$5.handle(FXMLDocumentController.java:3)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6867)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3311)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3151)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3106)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2248)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
at com.sun.glass.ui.View.notifyMouse(View.java:924)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)

Any help would be much appreciated.

Thank you in advance.

EDIT\ Fixed the loader. Again, thanks Uchiha Itachi.

Community
  • 1
  • 1
Akatzuki
  • 1
  • 3
  • Just do it the same way as the first example in the [Dependency Injection](http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310) thread you referenced. Your code is very different to that code. – James_D May 17 '14 at 13:40

1 Answers1

1

Your error is basically because of the below line

FXMLLoader loader = FXMLLoader.load(getClass().getClassLoader().
                              getResource("filetree/childWindow.fxml"), resources);

Here, you are trying to create a FXMLLoader reference, but instead you have used the load() of the FXMLLoader.

FXMLLoader's load() will load the Pane which is used as the root in the FXML. So, instead of using

FXMLLoader.load()

you must directly use

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getClassLoader().
                              getResource("filetree/childWindow.fxml"), resources);
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176