Over a million developers have joined DZone.

Use JavaFX2 with Spring

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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 });
	}

	@Override
	public void start(Stage stage) throws Exception {
		root = (Node) this.context.getBean(rootName!=null?rootName:"root");
		startFXApplication(stage);
	}
	
	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() {
		super("main.xml");
	}

	public static void main(final String[] args) {
		Application.launch(args);
	}

	@Override
	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();
	    root.getItems().addAll(leafs);	    
		group.getChildren().add(root);
		stage.setScene(scene);
		stage.show();
	}

} 

 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;
	}
} 

 and

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"));
		this.getChildren().add(content);
	}

} 

 and

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"));
		this.getChildren().add(content);
	}

} 

 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"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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">
			<list>
				<ref bean="leafOne" />
				<ref bean="leafTwo" />
			</list>
		</property>
	</bean>
	<bean id="leafOne" class="org.jacp.JavaFXSpring.LeafOne"/>
	<bean id="leafTwo" class="org.jacp.JavaFXSpring.LeafTwo"/>
</beans> 

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.

 

 

 

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

Published at DZone with permission of Andy Moncsek. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}