簡體   English   中英

將控制器放在嵌套的FXML文件中時出現LoadException

[英]Getting a LoadException when I put controller in nested FXML file

在我將fx:controller屬性放入嵌套的fxml文件中之前,該應用程序運行正常。

主班:

@SpringBootApplication
public class MyApplication extends Application{

    private Stage primaryStage;
    private AnchorPane rootLayout;

    public static void main(String[] args) {

        SpringApplication.run(MyApplication.class, args);
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Survey Creator");
        initRootLayout();
    }

    private void initRootLayout() {
        try {
            String pathToCss = "survey-generator/out/production/classes/css/Default.css";
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MyApplication.class.getResource("/view/MainLayout.fxml"));
            rootLayout = loader.load();
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            scene.getStylesheets().add(pathToCss);
            primaryStage.setResizable(false);
            primaryStage.show();
        }

        catch(Exception e){
            throw new RuntimeException(e);
        }
    }
}

ClientController類:

import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;


public class ClientController {
    @FXML
    private ComboBox<String> clientList;

    @FXML
    public void initialize(){
        clientList = null;
    }

    public ClientController(ComboBox<String> clientList) {
        this.clientList = clientList;
    }
}

FXML:

<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" prefHeight="475.0" prefWidth="923.0"
            fx:controller="com.surveycreator.controllers.MainController">
    <children>
        <fx:include source="MyText.fxml"/>
        <VBox alignment="CENTER" layoutX="134.0" layoutY="48.0" prefHeight="100.0" prefWidth="668.0">
            <children>
                <fx:include source="ClientComboBox.fxml" fx:id="clientList"/>
                //more stuff below

ClientCombo.fxml-正常工作,直到我添加"fx:controller="com.app.controllers.ClientController"

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

<?import javafx.scene.control.ComboBox?>

<ComboBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" prefHeight="25.0" prefWidth="519.0"
          promptText="Select Client"/> 

將上面的控制器添加到ClientCombo.fxml后,出現以下錯誤:

Caused by: java.lang.RuntimeException: javafx.fxml.LoadException: 
/C:/Users/myusername/IdeaProjects/survey-generator/out/production/classes/view/ClientComboBox.fxml:7
/C:/Users/myusername/IdeaProjects/survey-generator/out/production/classes/view/MainLayout.fxml:18

    at com.app.MyApplication.initRootLayout(MyApplication.java:45)
    at com.app.MyApplication.start(MyApplication.java:28)

您在春季不管理fxml上下文,啟動時應將其委托給春季,因此請嘗試使用此方法

@SpringBootApplication
public class AirQualityFxApplication extends Application {

    private ConfigurableApplicationContext context;
    private Parent rootNode;

    @Override
    public void init() throws Exception {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(AirQualityFxApplication.class);
        context = builder.run(getParameters().getRaw().toArray(new String[0]));

        FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
        loader.setControllerFactory(context::getBean);
        rootNode = loader.load();
    }


    @Override
    public void start(Stage primaryStage) throws Exception {
        Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
        double width = visualBounds.getWidth();
        double height = visualBounds.getHeight();

        primaryStage.setScene(new Scene(rootNode, width, height));
        primaryStage.centerOnScreen();
        primaryStage.show();
    }

    @Override
    public void stop() throws Exception {
        context.close();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM