JavaFX 2.0 loading fxml files with event handlers fails

若如初见. 提交于 2019-12-20 02:39:07

问题


i am trying to load a fxml from a subfolder, but it fails. I have the line that make the replace scene content:

private Parent replaceSceneContent(String fxml) throws Exception {
        Parent page = (Parent) FXMLLoader.load(App.class.getResource("skinFolder/fxml/"+fxml), null, new JavaFXBuilderFactory());
        Scene scene = stage.getScene();
        if (scene == null) {
            scene = new Scene(page, 700, 450);
            scene.getStylesheets().add(App.class.getResource("skinFolder/css/defaultSkin.css").toExternalForm());
            stage.setScene(scene);
        } else {
            stage.getScene().setRoot(page);
        }
        stage.sizeToScene();
        return page;
    }

I use this function in the next method:

private void gotoLogin() {
        try {
            replaceSceneContent("login.fxml");
        } catch (Exception ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

The problem is that i got the next errors:

javafx.fxml.LoadException: Method processLogin() does not have the correct signature for an event handler.
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(Unknown Source)
    at javafx.fxml.FXMLLoader.processEndElement(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at Main.App.replaceSceneContent(App.java:115)
    at Main.App.gotoLogin(App.java:108)
    at Main.App.start(App.java:72)
    at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$3.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:722)

Anyone has any ideea what i need to fix so i can make this replace scene content to work?

Thanks


回答1:


If you are defining an action of Button in FXML file like this:

 <Button text="Login" onAction="#processLogin"/>

then you must define a method in the controller class like as following. Note the signature of processLogin:

    @FXML
    private void processLogin(javafx.event.ActionEvent event) {
        // Process Login
    }


来源:https://stackoverflow.com/questions/10411164/javafx-2-0-loading-fxml-files-with-event-handlers-fails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!