我已经阅读了关于传递参数的下一个答案,但它们对我没有帮助。
下面是我努力从事件处理程序打开新的FXML视图,该处理程序很容易传递参数。
请寻求帮助。
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>
此代码正确打开新Pane
但会引发下一个异常。此外,子窗格中的Label
也未显示。
NPE来自下一行: bordermanagetemp.getChildren()。add((Node)fxmlloader.load(url.openStream()));
堆栈跟踪:
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
答案 0 :(得分:1)
您提供的链接中包含所有内容。对于那些对How to pass data between Controllers
和也有疑问的人来说,这是最好的答案之一!
但是,在你的情况下,我会建议在传递参数中使用Binding
!您可以在id
中添加ChildController
,然后将其绑定到MainController's id
。这样id
的值就会在控制器之间自动传递
您可以查看以下网址以便更好地理解,或者如果您可以显示,您如何在MainController中获取ID,可能我可以帮助您使用某些代码!
http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm
http://www.drdobbs.com/jvm/javafx-20-binding/231903245
编辑 - 从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);
}
答案 1 :(得分:1)
异常说FXMLLoader
无法通过反射访问您的ManageTemplateChildController类本身或其字段/方法。让你的课程公开。将@FXML
注释添加到在FXML文件中定义为“fx:id”的私有字段,或者将这些字段公开,而不添加@FXML注释。