Quarkus FX

This extension allows you to use Java FX in your Quarkus application.

Installation

In order to use this extension, you need to add the io.quarkiverse.fx:quarkus-fx dependency to your project.

Maven
<dependency>
    <groupId>io.quarkiverse.fx</groupId>
    <artifactId>quarkus-fx</artifactId>
    <version>0.10.3</version>
</dependency>
Gradle
dependencies {
    implementation 'io.quarkiverse.fx:quarkus-fx:0.10.3'
}

Usage

The extension allows using CDI features in JavaFX controller classes.
The FXMLLoader is made a CDI bean and can be injected in your application.

Loading FXML with injected FXMLLoader
@Inject
FXMLLoader fxmlLoader;

public void load() {
  Parent root = this.fxmlLoader.load(this.getClass().getResourceAsStream("/app.fxml"));
  Scene scene = new Scene(root);
  stage.setScene(scene);
  stage.show();
}
Controller
public class MyFxmlController {

  @Inject
  MyService myService;

  @FXML
  private void onButtonClicked() {
    this.myService.doSomething();
  }
}

Startup

The application will automatically be launched (thanks to a call to javafx.application.Application::launch) when the extension is present.

If you need to customize the launch, you can provide a custom @QuarkusMain, such as :

Custom main
package io.quarkiverse.fx.fxapp;

import io.quarkiverse.fx.FxApplication;
import io.quarkus.runtime.QuarkusApplication;
import io.quarkus.runtime.annotations.QuarkusMain;
import javafx.application.Application;

@QuarkusMain
public class QuarkusFxApplication implements QuarkusApplication {
    @Override
    public int run(String... args) {
        Application.launch(FxApplication.class, args);
        return 0;
    }
}

When the application is started, two main events are fired in sequence :

  • FxApplicationStartupEvent indicates that the Application is created and available,

  • FxPostStartupEvent indicates that the Application is ready as well as the Stage instance.

void onApplicationStartup(@Observes FxApplicationStartupEvent event) {
  Application application = event.getApplication();
}
void onPostStartup(@Observes FxPostStartupEvent event) {
  Stage stage = event.getPrimaryStage();
}

Injecting HostServices

The HostServices instance which is obtained via Application::getHostServices can also be injected by using the @Inject annotation.

@ApplicationScoped
public class MyBean {

  @Inject
  HostServices hostServices;
}

Accessing the primary stage

The primary Stage can be accessed by injecting FxViewRepository.

@ApplicationScoped
public class MyBean {

  @Inject
  FxViewRepository viewRepository;

  void doSomething() {
    Stage primaryStage = this.viewRepository.getPrimaryStage();
    primaryStage.setTitle("App Title");
  }
}

Properties

Configuration property fixed at build time - All other configuration properties are overridable at runtime

Configuration property

Type

Default

Root location for fx views. The extension will look for fx views from this root directory.

Environment variable: QUARKUS_FX_VIEWS_ROOT

string

/

Stylesheet live reload strategy.

Environment variable: QUARKUS_FX_STYLESHEET_RELOAD_STRATEGY

always, dev, never

dev

Location for source resources (allowing stylesheet live reload in dev mode)

Environment variable: QUARKUS_FX_SOURCE_RESOURCES

string

src/main/resources/

Location for target resources (where resources files are located after build) In dev mode, if stylesheet reload is activated, app will use stylesheet from sources instead of the ones in target and monitor changes

Environment variable: QUARKUS_FX_TARGET_RESOURCES

string

target/classes/

Native support

Initial native support has been added, to build an executable with GraalVM.

Generate reachability metadata

The extension automatically configures reflection and runtime initialization of the JavaFX components, for supported operating systems (Windows, Mac & Linux).

You will also need to build your application reachability metadata. To do so, you can :

Run the application using GraalVM to collect reachability metadata:

Example on Windows
%GRAALVM_HOME%\bin\java.exe -agentlib:native-image-agent=config-output-dir=tracing-output -jar quarkus-run.jar

Try to load all UI components in the application and do all possible interactions. Upon exit, tracing-output/reachability-metadata.json will be generated.

Copy tracing-output/reachability-metadata.json to src/main/resources/META-INF/native-image/{groupId}/{artifactId}/reachability-metadata.json

Build executable

Environment variable GRAALVM_HOME has to be set, pointing to your GraalVM installation.

In order to build the application as a native executable, you can run the following maven command :

mvn install -Dnative
Please report any found bug by opening an issue at https://github.com/quarkiverse/quarkus-fx/issues