如何将 fxml 加载器的位置设置为另一个包中的 fxml
How to set the fxml loader's location to an fxml in another package
我正在尝试制作一个具有用于制作新项目的对话框的应用程序。我已经对它进行了编程,但想清理文件结构,所以我将对话框及其控制器的 fxml 移到了它们自己的包中。对话框的文件位于名为 newItemDialog 的包中。当我尝试启动它时,出现 java.lang.IllegalStateException: Location is not set 错误。我已经在 how to locate fxml from another package?
尝试过解决方案
这是我的项目文件结构的图片:
Project File Structure
这是启动对话框的代码
@FXML
public void showNewItemDialog() {
Debug.getInstance().log("Entering showNewItemDialog method", false);
Dialog<ButtonType> dialog = new Dialog<>();
dialog.initOwner(mainBorderPane.getScene().getWindow());
dialog.setTitle("Create new item");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/newItemDialog/newToDoItem.fxml"));
try {
dialog.getDialogPane().setContent(fxmlLoader.load());
} catch(IOException e) {
Debug.getInstance().log("An error has occurred in the showNewItemDialog method\n", true);
e.printStackTrace();
return;
}
dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
Optional<ButtonType> result = dialog.showAndWait();
if(result.isPresent() && result.get() == ButtonType.OK) {
Debug.getInstance().log("Updating ListView", false);
NewItemDialogController controller = fxmlLoader.getController();
ToDoItem newItem = controller.processResults();
toDoListView.getSelectionModel().select(newItem);
}
}
谢谢!
在大多数情况下,.fxml
文件的路径设置不正确。您可以尝试设置正确的路径,例如:
1.使用绝对路径:
FXMLLoader loader = new XMLLoader(getClass().getResource("/com/vincent/todo/newItemDialog/newToDoItem.fxml"));
2。使用相对路径:
FXMLLoader loader = new XMLLoader(getClass().getResource("newItemDialog/newToDoItem.fxml"));
3。使用同一包中的 class:
FXMLLoader loader = new XMLLoader(NewItemDialogController.class.getResource("newToDoItem.fxml"));
您应该使用 getClass().getResource("newItemDialog/newToDoItem.fxml")
而不是第一个斜杠。
我正在尝试制作一个具有用于制作新项目的对话框的应用程序。我已经对它进行了编程,但想清理文件结构,所以我将对话框及其控制器的 fxml 移到了它们自己的包中。对话框的文件位于名为 newItemDialog 的包中。当我尝试启动它时,出现 java.lang.IllegalStateException: Location is not set 错误。我已经在 how to locate fxml from another package?
尝试过解决方案这是我的项目文件结构的图片: Project File Structure
这是启动对话框的代码
@FXML
public void showNewItemDialog() {
Debug.getInstance().log("Entering showNewItemDialog method", false);
Dialog<ButtonType> dialog = new Dialog<>();
dialog.initOwner(mainBorderPane.getScene().getWindow());
dialog.setTitle("Create new item");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/newItemDialog/newToDoItem.fxml"));
try {
dialog.getDialogPane().setContent(fxmlLoader.load());
} catch(IOException e) {
Debug.getInstance().log("An error has occurred in the showNewItemDialog method\n", true);
e.printStackTrace();
return;
}
dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
Optional<ButtonType> result = dialog.showAndWait();
if(result.isPresent() && result.get() == ButtonType.OK) {
Debug.getInstance().log("Updating ListView", false);
NewItemDialogController controller = fxmlLoader.getController();
ToDoItem newItem = controller.processResults();
toDoListView.getSelectionModel().select(newItem);
}
}
谢谢!
在大多数情况下,.fxml
文件的路径设置不正确。您可以尝试设置正确的路径,例如:
1.使用绝对路径:
FXMLLoader loader = new XMLLoader(getClass().getResource("/com/vincent/todo/newItemDialog/newToDoItem.fxml"));
2。使用相对路径:
FXMLLoader loader = new XMLLoader(getClass().getResource("newItemDialog/newToDoItem.fxml"));
3。使用同一包中的 class:
FXMLLoader loader = new XMLLoader(NewItemDialogController.class.getResource("newToDoItem.fxml"));
您应该使用 getClass().getResource("newItemDialog/newToDoItem.fxml")
而不是第一个斜杠。