在我将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)
答案 0 :(得分:0)
您在春季不管理fxml上下文,启动时应将其委托给spring,因此请尝试使用
@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();
}
}