Question

I already read next answers about passing parameters, but they didn't help me.

Below is my effort to open new FXML view from event handler which failes to pass parameter.

Kindly looking for help.

MainController.java

@FXML
private void handleButtonAction(ActionEvent event) throws IOException 
{

    Context currentContext = new Context();
    URL url = getClass().getResource("ManageTemplateChild.fxml");
    FXMLLoader fxmlloader = new FXMLLoader();
    fxmlloader.setLocation(url);
    fxmlloader.setBuilderFactory(new JavaFXBuilderFactory());
    bordermanagetemp.getChildren().clear();
    bordermanagetemp.getChildren().add((Node) fxmlloader.load(url.openStream()));

    ((ManageTemplateChildController) fxmlloader.getController())
         .setContext(currentContext,Finalvaluetablerow);
}

ManageTemplateChildController.java

private Context context;
public void setContext(Context context,Object Finalvaluetablerow)
{
    this.context = context;
    o = Finalvaluetablerow;
    System.out.println("value of main" + Finalvaluetablerow + " value of child" + o);
    testlabel.setText((String) o);
}

Context.java

public class Context {
    private final static Context instance = new Context();

    public static Context getInstance() {
        return instance;
    } 
}

ManageTemplateChild.FXML

<!-- language: xml -->
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<BorderPane fx:id="bordernew" prefHeight="497.0" prefWidth="642.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="customercloudfx.ManageTemplateChildController">
  <center>
    <AnchorPane prefHeight="200.0" prefWidth="200.0">
      <children>
        <Label fx:id="testlabel" layoutX="153.0" layoutY="168.0" text="Label">
          <font>
            <Font size="100.0" />
          </font>
        </Label>
      </children>
    </AnchorPane>
  </center>
</BorderPane>

This code correctly opens new Pane but throws next exception. Also Label in the child pane is not shown.

NPE comes from next line: bordermanagetemp.getChildren().add((Node) fxmlloader.load(url.openStream()));

StackTrace:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1440)
    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:6863)
    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:3324)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3164)
    at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3119)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1559)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2261)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:228)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
    at com.sun.glass.ui.View.notifyMouse(View.java:922)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1435)
    ... 44 more
Caused by: java.lang.RuntimeException: java.lang.IllegalAccessException: Class javafx.fxml.FXMLLoader$ValueElement can not access a member of class customercloudfx.ManageTemplateChildController with modifiers ""
    at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:661)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:572)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2314)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2131)
    at customercloudfx.ManageTemplateController.handleButtonAction(ManageTemplateController.java:127)
    ... 49 more
Caused by: java.lang.IllegalAccessException: Class javafx.fxml.FXMLLoader$ValueElement can not access a member of class customercloudfx.ManageTemplateChildController with modifiers ""
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
    at java.lang.reflect.Field.doSecurityCheck(Field.java:983)
    at java.lang.reflect.Field.getFieldAccessor(Field.java:927)
    at java.lang.reflect.Field.set(Field.java:680)
    at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:659)
    ... 53 more
Était-ce utile?

La solution

The exception says that the FXMLLoader cannot access to your ManageTemplateChildController class itself or to its fields/methods through reflection. Make your class public. Add @FXML annotation to private fields which are defined in FXML file as "fx:id", or alternatively make these fields public without adding @FXML annotation.

Autres conseils

The link that you have provided has everything in it. It is one of the best answers for those who have doubts on How to pass data between Controllers and an answer to your question as well !

But, in your case, I would suggest to use Binding inside of passing parameters ! You can have a id in your ChildController and you can just bind it to the MainController's id. So that the value of id is automatically passed between the controllers

You can have a look at the following URL for a better understanding or if you can show, how you getting the id in MainController, may be I can help you with some code !

http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm

http://www.drdobbs.com/jvm/javafx-20-binding/231903245

EDIT - To load controller from FXML

@FXML
private void handleButtonAction(ActionEvent event) throws IOException 
{
         //Instead of making a reference from the FXMLLoader, you are creating a new Object
         //ManageTemplateChildController m = new ManageTemplateChildController();
         //try using
         FXMLLoader loader = new FXMLLoader(getClass().getResource("ManageTemplateChild.fxml"));
         ManageTemplateChildController controller = 
                          loader.<ManageTemplateChildController>getController();
         //where loader is the FXMLLoader for you second FXML
         m.redirecthome(stage,Finalvaluetablerow);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top