Skip to content

fuinorg/javafx-cdi-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

javafx-cdi-example

JavaFX with CDI example.

Apache Java Development Kit 11

⚠️ In case you have a hi-resolution display and want to run the application on Ubuntu, make sure to pass an environment variable like GDK_SCALE=2to the application. Otherwise it will be unscaled. See Issue #643 for more details.

Features

The example application shows how to implement the most important parts of a real world JavaFX application. Most tutorials for JavaFX only show how to create a screen, but there is more than that.

This example has the following features included:

Based on Java 17 with JavaFX 21

The project is based on Java 17 and JavaFX.

Maven build

There is a fully configured Maven build that creates a ZIP archive that has a batch file (Windows) and a shell script (Linux) to start the application.

Github build

The project is ready configured to be build on Github. Simply copy the project generated by the below archetype to a fresh Github repository and you have an automated build out of the box.

It also sets the necessary arguments in case you want to run some GUI unit tests:

-Djava.awt.headless=true -Dtestfx.robot=glass -Dtestfx.headless=true 
-Dprism.order=sw -Dprism.text=t2k -Dtestfx.setup.timeout=2500

Contexts and Dependency Injection (CDI)

You can use @Inject annotations in your application controllers for injecting services, events and configuration.

@Singleton
@Loggable
public class ChildController implements Initializable {

    @FXML
    private Label labelResult;
    
    @Inject
    private Logger log;

    ...

}

Structuring an application with sub controllers

If you start with JavaFX it's not easy to figure out how to structure a larger application. See the JavafxCdiExampleController.java to see how a parent controller opens a child controller and passes some data to it.

@FXML
public void onOpenChildController(ActionEvent event) {
    final NodeControllerPair<ChildController> childCtrlPair = 
            NodeControllerPair.load(loaderInstance, ChildController.class);
    final ChildController childController = childCtrlPair.getController();
    childController.setProgressUI(progressUI);
    childController.setMessageDisplay(messageDisplay);
    borderPaneContent.setCenter(childCtrlPair.getParent());
}

Using tasks for handling long operations

There are often some operations that may take a while. You need to create a task for it. See the LongRunningTaskExample.java on how to do it. The task also emits some status information to the UI and blocks it.

Blocking UI during task execution

During execution of a long running task you most likely want to block the UI and show some progress indicator.

About dialog showing a version

Use the Maven version from the POM and the Git information inside the project to populate an About dialog.

Catching unexpected exceptions showing a dialog

Sometimes your application will fail and throw an exception. It's a good idea to show something useful to the user (See ExceptionAlert).

UI test with TestFX

Example UI test with TestFX: JavafxCdiExampleTest.java

Maven Archetype

There is a Maven Archetype available that helps kickstarting your own project based on this code.

Releases

No releases published

Packages

No packages published

Languages