0

I am using JavaFX 2 in conjunction with the Spring Framework, however the injection happens to late. My Controller is instanciated by the FXML-Loader, the Spring injection for member variables of this Controller works, but it works only too late, this means that in (1) injection yet didn't happen and in (2) injection did happen:

public class MainController extends AbstractController
{
    @Autowired
    public StatusBarController statusbarController;

    // Implementing Initializable Interface no longer required according to
    // http://docs.oracle.com/javafx/2/fxml_get_started/whats_new2.htm:
    private void initialize() {
        BorderPane borderPane = (BorderPane)getView();        
        borderPane.setBottom(statusbarController.getView()); // (1) null exception!
    }

    // Linked to a button in the view
    public void sayHello() {
        BorderPane borderPane = (BorderPane)getView();        
        borderPane.setBottom(statusbarController.getView()); // (2) works!
    }
}

Any way to let Spring inject statusbarController in an earlier state? I can't let the user have to click a button to load my GUI ;-)

My AppFactory is this:

@Configuration
public class AppFactory 
{
    @Bean
    public MainController mainController() throws IOException
    {
        return (MainController) loadController("/main.fxml");
    }

    protected Object loadController(String url) throws IOException
    {
        InputStream fxmlStream = null;
        try
        {
            fxmlStream = getClass().getResourceAsStream(url);
            FXMLLoader loader = new FXMLLoader();
            Node view = (Node) loader.load(fxmlStream);
            AbstractController controller = (AbstractController) loader.getController();
            controller.setView(view);
            return controller;            
        }
        finally
        {
            if (fxmlStream != null)
            {
                fxmlStream.close();
            }
        }
    }
}
4

1 に答える 1

2

コントローラ インスタンスの作成を担当できるように、FXMLLoader に ControllerFactory を設定する必要があります。

于 2013-02-12T13:17:11.250 に答える