---
title: 'JavaFX + CDI (Weld)'
url: 'https://tutego.de/blog/javainsel/2013/10/javafx-cdi-weld'
markdown: 'https://tutego.de/blog/javainsel/2013/10/javafx-cdi-weld.md'
date: '2013-10-18'
description: 'Put in the class path: The challenge is to bring FXMLLoader and CDI together, because JavaFX is creating naked objects by itself, now they have to be „CDI-aware“. package tutego.fx; import java.nio.charset.*; import javafx.fxml.FXMLLoader; import javafx.util.Callback; import javax.enterpri…'
taxonomy:
  category:
    - JavaFX
    - CDI
    - Weld
---

# JavaFX + CDI (Weld)

![Illustration einer tropischen Insel mit Palmen und einem hohen Berg](https://tutego.de/blog/javainsel/user/themes/tutego/images/insel-cover.jpg)# Java Blog für Programmierer

[Java Blog für Programmierer](https://tutego.de/blog/javainsel/)

## JavaFX + CDI (Weld)

18.10.2013 · [Christian Ullenboom](https://tutego.de/blog/javainsel/author/admin/)

Put in the class path:

- 
- 
- 

The challenge is to bring `FXMLLoader` and CDI together, because JavaFX is creating naked objects by itself, now they have to be „CDI-aware“.

```java
package tutego.fx;

import java.nio.charset.*;
import javafx.fxml.FXMLLoader;
import javafx.util.Callback;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

public class FXMLLoaderProducer
{
  @Inject
  Instance instance;

  @Produces
  public FXMLLoader createLoader()
  {
    return new FXMLLoader( null, null, null, new Callback

```

In the `FXML`-file there is a reference to the FX Controller. JavaFX has to load it and can make the injections with the help of our very first class. A regular service is getting injected into the controller:

```java
package tutego.fx;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javax.inject.Inject;

public class RandomController
{
  @FXML
  private Label label;

  @Inject
  private RandomService randomService;

  @FXML
  public void onButtonClick()
  {
    label.setText( "Random " + randomService.nextInt() );
  }
}
```

The service itself is a simple singleton:

```java
package tutego.fx;

import java.util.Random;
import javax.inject.Singleton;

@Singleton
public class RandomService
{
  private Random rnd = new Random();

  public int nextInt()
  {
    return rnd.nextInt();
  }
}
```

Thats it!

PS: When you start, dont forget to put a (even blank) *beans.xml* in *META-INF*.

Kategorien: [JavaFX](https://tutego.de/blog/javainsel/category/javafx/), [CDI](https://tutego.de/blog/javainsel/category/cdi/), [Weld](https://tutego.de/blog/javainsel/category/weld/)

## 2 Kommentare

**Brian Mooney** · [08.02.2015 01:42](#comment-81188)

Nice article. Helped me a lot to get a small app that I am working on bootstrapped. Just one point for anyone that may hit the same issue as myself. I wanted to load the FXML files via a URL. Unfortunately the load(URL) on FXMLLoader is a static (which internally creates a new FXMLLoader anyways which defeats the purpose). So to avoid having to have InputStreams in my code, you can use two isntance methods, setLocation(URL url) and load(): ```java @Inject private FXMLLoader fxmlLoader; @Inject private ResourceAccessor resourceAccessor; public void start(Stage primaryStage) throws IOException { fxmlLoader.setLocation(resourceAccessor .getResource(ResourceAccessor.FXML\_LANDING\_PAGE)); Parent root = (Parent) fxmlLoader.load(); Scene scene = new Scene(root, 400, 400); scene.getStylesheets().add( resourceAccessor.getResource(ResourceAccessor.CSS\_APP) .toExternalForm()); primaryStage.setScene(scene); primaryStage.show(); } ```

**Oliver Welter** · [21.12.2014 03:33](#comment-69347)

Thanks for the article, it helps a lot! I stumbled over an issue with the shown code if I try to load an fxml file which references stylesheets via ... ... In this case I got an NoSuchMethodException for java.net.URL.() . This can be solved by handing over an instance of JavaFXBuilderFactory when creating the FXMLLoader in FXMLLoaderProducer.java : import java.nio.charset.StandardCharsets; import javafx.fxml.FXMLLoader; import javafx.fxml.JavaFXBuilderFactory; import javafx.util.Callback; import javax.enterprise.inject.Instance; import javax.enterprise.inject.Produces; import javax.inject.Inject; public class FXMLLoaderProducer { @Inject Instance instance; @Produces public FXMLLoader createLoader() { return new FXMLLoader(null, null, new JavaFXBuilderFactory(), new Callback<Class, Object>() { @Override public Object call(Class param) { return instance.select(param).get(); } }, StandardCharsets.UTF\\\_8); } }

Die bisherigen Kommentare wurden aus WordPress übernommen. Neue Kommentare sind derzeit geschlossen.

[← Alle Beiträge](https://tutego.de/blog/javainsel/)

---

## Navigation

- Previous: [Oracle Java SE 7u45 Update](https://tutego.de/blog/javainsel/2013/10/oracle-java-se-7u45-update.md)
- Next: [NetBeans IDE 7.4 mit JDK 8 und mehr](https://tutego.de/blog/javainsel/2013/10/netbeans-ide-7-4-jdk-8.md)
