Over a million developers have joined DZone.

Use JavaFX2 with Spring

DZone's Guide to

Use JavaFX2 with Spring

· Java Zone ·
Free Resource

"I love writing authentication and authorization code." ~ No Developer Ever. Try Okta Instead.

Spring is more than a simple IoC container, it gives you access to a whole platform. Besides the benifits of injecting Nodes or parts of your application UI you can benefit of the whole Spring infrastructure. The following examle demonstrates how to define a JavaFX2 / Spring application.

To start a JavaFX 2 application we usually extend from javafx.application.Application and implement a main method to call the static Application.launch() method. Defining a *.Application bean didn’t succeed for me so let’s create an abstract launcher class which extends from javafx.application.Application and contains the Spring context:

 public abstract class ASpringFX extends Application {
	private ClassPathXmlApplicationContext context = null;
	private final String springXML;
	private Node root;
	private final String rootName;

	public ASpringFX(final String springXML) {
		this.springXML = springXML;
		this.rootName = null;
		this.context = new ClassPathXmlApplicationContext(
				new String[] { springXML });

	public void start(Stage stage) throws Exception {
		root = (Node) this.context.getBean(rootName!=null?rootName:"root");
	public String getSpringXML() {
		return springXML;

	public abstract void startFXApplication(Stage stage);

The start(Stage stage) method I use to get the root bean defined in the Spring context and to pass the stage to an abstract method. You can easilly adjust the code and pass the root node too…

Now we need a Class with a main method that is bootstrapping the JavaFX Application and the Spring context. 

public class SpringFX extends ASpringFX{
	public SpringFX() {

	public static void main(final String[] args) {

	public void startFXApplication(Stage stage) {
		Group group = new Group();
	    Scene scene = new Scene(group, 800, 600, Color.BLACK);		    
	    RootComponent root = (RootComponent) this.getRoot();
	    List<Node> leafs = root.getLeafs();


 You can see “this.getRoot()” returning the root node from Spring context. This component is a SplitPane and contains the leaf Nodes injected by Spring.

 So create the root and the leaf components :

public class RootComponent extends SplitPane {
	private List<Node> leafs;
	public RootComponent() {
		this.setPrefSize(800, 600);

	public List<Node> getLeafs() {
		return leafs;

	public void setLeafs(List<Node> leafs) {
		this.leafs = leafs;


public class LeafOne extends GridPane {
	public LeafOne() {
		Pane content = new Pane();
		GridPane.setHgrow(content, Priority.ALWAYS);
		GridPane.setVgrow(content, Priority.ALWAYS);
		content.getChildren().add(new Label("left"));



public class LeafTwo extends GridPane {
	public LeafTwo() {
		Pane content = new Pane();
		GridPane.setHgrow(content, Priority.ALWAYS);
		GridPane.setVgrow(content, Priority.ALWAYS);
		content.getChildren().add(new Label("right"));


 Finaly create a spring.xml and define the beans (I prefer xml to show the hierarchie)…

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="root" class="org.jacp.JavaFXSpring.RootComponent">
		<property name="leafs">
				<ref bean="leafOne" />
				<ref bean="leafTwo" />
	<bean id="leafOne" class="org.jacp.JavaFXSpring.LeafOne"/>
	<bean id="leafTwo" class="org.jacp.JavaFXSpring.LeafTwo"/>

And voilà … you defined JavaFX 2 Nodes as Spring beans. You can use the root as well as the leaf nodes like avarage Spring beans and use the whole Spring infrastructure.




"I love writing authentication and authorization code." ~ No Developer Ever. Try Okta Instead.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}