[英]call other class method from controller in javafx
I am currently doing an app in JavaFx and in that I am calling main class method from Controller class.I have check sample example provided by Oracle and in that example it's working fine. 我目前正在用JavaFx编写一个应用程序,并且正在从Controller类调用主类方法。我有Oracle提供的检查示例示例,并且在该示例中工作正常。 Here it's my code: Main class: 这是我的代码:主类:
public class Boiler_New extends Application {
private Stage stage;
@Override
public void start(Stage primarystage) throws Exception {
stage = primarystage;
stage.setTitle("Login");
call1("Login.fxml");
stage.show();
}
public void call1(String fxml) throws IOException{
FXMLLoader loader = new FXMLLoader();
InputStream in = Boiler_New.class.getResourceAsStream(fxml);
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(Boiler_New.class.getResource(fxml));
AnchorPane root = (AnchorPane)loader.load(in);
Scene scene = new Scene(root);
stage.setScene(scene);
}
public static void main(String[] args) {
launch(args);
}
}
Controller: 控制器:
public class LoginController extends AnchorPane implements Initializable {
private Boiler_New application ;
@FXML
private Label label;
@FXML
private AnchorPane login_panel;
@FXML
private Button login_button;
@FXML
private ImageView close_button;
public void setApp(Boiler_New application){
this.application = application;
}
@FXML
public void login_act(ActionEvent event) throws Exception {
application.call1("content.fxml");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Tracing: 跟踪:
Executing boiler_new.Boiler_New from C:\Users\BlackWorld\Documents\NetBeansProjects \Boiler_New\dist\run1175007848\Boiler_New.jar using platform C:\Program Files\Java\jdk1.7.0_25/bin/java
***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.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.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:3328)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
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:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Thread.java:724)
***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:606)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1435)
... 44 more
***Caused by: java.lang.NullPointerException***
at boiler_new.LoginController.login_act(LoginController.java:50)
... 54 more
Exception in thread "JavaFX Application Thread" Deleting directory C:\Users\BlackWorld\Documents\NetBeansProjects\Boiler_New\dist\run1175007848
Please help me. 请帮我。
I have faced the same problem, to solve it do the following : 我遇到了同样的问题,要解决此问题,请执行以下操作:
Add a new function in LoginController as below : 在LoginController中添加一个新函数,如下所示:
private void openNewWindow(Event event) { Parent root; try { URL url = getClass().getResource("Content.fxml"); FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setLocation(url); fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory()); root = (Parent)fxmlLoader.load(url.openStream()); Stage stage = new Stage(); stage.setScene(new Scene(root, 1000, 800)); stage.show(); ((Node)(event.getSource())).getScene().getWindow().hide(); } catch (IOException e) { e.printStackTrace(); } }
Call this function instead of application.call1("content.fxml"); 调用此函数,而不是application.call1(“ content.fxml”);
@FXML public void login_act(ActionEvent event) throws Exception { openNewWindow(event); }
I am sure it works fine. 我确信它能正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.