JavaFX/Spring自定义onViewChange方法

JavaFX/Spring自定义onViewChange方法,java,spring,user-interface,javafx,Java,Spring,User Interface,Javafx,我正在使用一个Spring应用程序,使用JavaFX。我创建了一个静态类,可以轻松地更改视图 现在我需要在给定视图退出时调用该视图的方法(类似于调用视图时的initializemethod)。许多视图可以调用许多其他视图,因此每当我单独调用SpringFxmlLoader时,调用这样的方法似乎很难有效 是否有一种方法可以在当前加载的控制器上调用WindowChange之前的自定义,然后加载新视图?前提是JavaFX上没有用于此目的的现有方法 我可以确保WindowChange之前的存在于我项目的

我正在使用一个Spring应用程序,使用JavaFX。我创建了一个静态类,可以轻松地更改视图

现在我需要在给定视图退出时调用该视图的方法(类似于调用视图时的
initialize
method)。许多视图可以调用许多其他视图,因此每当我单独调用SpringFxmlLoader时,调用这样的方法似乎很难有效

是否有一种方法可以在当前加载的控制器上调用WindowChange之前的自定义
,然后加载新视图?前提是JavaFX上没有用于此目的的现有方法

我可以确保WindowChange之前的
存在于我项目的每个视图控制器上,而不会出现问题

我的自定义加载程序类:

public class SpringFxmlLoader {
    /**
     * Generic Window Loader for Project Overrides Controller Factory for Spring
     * and JavaFX Integration
     *
     * @param applicationContext - Spring application Context
     * @param anchorPane - Active view container.
     * @param resource - FXML loaded resource.
     * @throws IOException
     */
    public static void changeWindow(ApplicationContext applicationContext, AnchorPane anchorPane, URL resource) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(resource);
        fxmlLoader.setControllerFactory(applicationContext::getBean);
        // todo: get currently loaded controller from anchorPane
        // todo: call onWindowChange custom method
        Parent root = fxmlLoader.load();
        Stage stage = (Stage) anchorPane.getScene().getWindow();
        stage.setScene(new Scene(root, 800, 600));
        stage.show();
    }
}

非常感谢您的帮助:)

您可以这样做:

public class SpringFxmlLoader {
    /**
     * Generic Window Loader for Project Overrides Controller Factory for Spring
     * and JavaFX Integration
     *
     * @param applicationContext - Spring application Context
     * @param anchorPane - Active view container.
     * @param resource - FXML loaded resource.
     * @throws IOException
     */

    private static final Map<Parent, Object> controllers = new HashMap<>();

    public static void changeWindow(ApplicationContext applicationContext, AnchorPane anchorPane, URL resource) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(resource);
        fxmlLoader.setControllerFactory(applicationContext::getBean);

        Object controller = controllers.get(anchorPane);
        if (controller != null) {
            // do what you need to do with the controller...
        }

        Parent root = fxmlLoader.load();

        controllers.put(anchorPane, fxmlLoader.getController());

        Stage stage = (Stage) anchorPane.getScene().getWindow();
        stage.setScene(new Scene(root, 800, 600));
        stage.show();
    }
}
当然,您可以在任何需要的地方注入
springfxmloader

// not sure how you are configuring Spring, but with annotation-based
// configuration it would look like
@Scope("singleton") // not really needed as this is the default
@Bean
public class SpringFxmlLoader {

    @Autowired
    private ApplicationContext applicationContext ;

    /**
     * Generic Window Loader for Project Overrides Controller Factory for Spring
     * and JavaFX Integration
     *
     * @param anchorPane - Active view container.
     * @param resource - FXML loaded resource.
     * @throws IOException
     */

    private final Map<Parent, Object> controllers = new HashMap<>();

    public void changeWindow(AnchorPane anchorPane, URL resource) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(resource);
        fxmlLoader.setControllerFactory(applicationContext::getBean);

        Object controller = controllers.get(anchorPane);
        if (controller != null) {
            // do what you need to do with the controller...
        }

        Parent root = fxmlLoader.load();

        controllers.put(anchorPane, fxmlLoader.getController());

        Stage stage = (Stage) anchorPane.getScene().getWindow();
        stage.setScene(new Scene(root, 800, 600));
        stage.show();
    }
}