在几个地方,RootLayoutController
需要引用MainApp
类。我们也没有传递一个MainApp
的引用到RootLayoutController
。
打开MainApp
类,使用下面的替代initRootLayout()
方法:
* Initializes the root layout and tries to load the last opened
* person file.
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class
.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
// Give the controller access to the main app.
RootLayoutController controller = loader.getController();
controller.setMainApp(this);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
// Try to load last opened person file.
File file = getPersonFilePath();
if (file != null) {
loadPersonDataFromFile(file);
注意两个修改:一行*给控制器访问MainApp*和最后三行*加载最新打开的人员文件*。
做应用程序的测试驱动,你应该能够使用菜单保存人员数据到文件中。
当你在编辑器中打开一个xml
文件,你将注意到生日没有正确保存,这是一个空的<birthday/>
标签。原因是JAXB不只奥如何转换LocalDate
到XML。我们必须提供一个自定义的LocalDateAdapter
定义这个转换。
在ch.makery.address.util
中创建新的类,称为LocalDateAdapter
,内容如下:
LocalDateAdapter.java