Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:36:55+00:00 2026-06-11T11:36:55+00:00

I am using JavaFX 2 with Scala. I have the class Application extends javafx.application.Application

  • 0

I am using JavaFX 2 with Scala.

I have the class Application extends javafx.application.Application which does things like reads app configuration, etc. Then it fires up the main window.

This main window needs to be connected to a controller called MainWindow. I can do it with fx:controller="com.foo.bar.MainWindow", however, in this case the instance of MainWindow is unable to access my application’s configuration, etc.

One thing that came to my mind is to instantiate the MainWindow controller on my own inside the Application class and inject all dependencies, and then tell the main window view that the controller should be the instance I just created. However, I have no idea how to do this?

What’s the preferred way to access data in controllers in JavaFX — via some sort of dependency injection or something else?

In other words, this view FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.Scene?>
<?import javafx.scene.control.*?>

<BorderPane fx:controller="com.foo.bar.MainWindow" prefHeight="703.0" prefWidth="803.0" xmlns:fx="http://javafx.com/fxml">
...

Will automatically instantiate a com.foo.bar.MainWindow controller for me(?):

...
class MainWindow {
  var app: Application = null
  ...
}

where I have the reference to my application, which holds information I want to access:

class Application extends javafx.application.Application {
  ...
  // Stuff I want to access, application also fires up the first scene.
}

The problem is that how can I get a reference to the main application? How can I do dependency injection or anything equivalent?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:36:56+00:00Added an answer on June 11, 2026 at 11:36 am

    Guice is my preferred way of injecting controllers, all you need to use the following controller factory:

    class GuiceControllerFactory implements Callback<Class<?>, Object> {
    
        private final Injector injector;
    
        public GuiceControllerFactory(Injector anInjector) {
            injector = anInjector;
        }
    
        @Override
        public Object call(Class<?> aClass) {
            return injector.getInstance(aClass);
        }
    }
    

    Set it on your FXMLLoader:

    FXMLLoader loader = new FXMLLoader();         
    loader.setControllerFactory(controllerFactory);
    

    Now Guice will create your controller. I recommend that you inject in any services your controller needs instead of passing in a reference to your application class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to change the application icon using JavaFX, or does it have
I have a JavaFX 2.0 application, which consists of two FXML files, and two
I have JavaFX application using FXML to build its GUI. When this application is
I have an application written on JavaFX (with using of FXML). I'm trying to
I have a Scala application using Akka that receives REST requests, makes some operations
Is it possible to specify a font using CSS in JavaFX application? I have
I have developed application in Netbeans IDE 6.9 Using javafx Script based Application. i
I have made sample application with JavaFX using netbeans 7.2. When I run application
I'm using the Task class from JavaFx's API to perform network operations. The class
I was working on a small application using a simple binding in JavaFx 2.1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.