Andy Moncsek is a software engineer and passionate Java developer since 2004. He enjoys programming with all kinds of JVM languages, frameworks, and operating systems. He is currently working for Trivadis AG Switzerland and in his spare time he is the project-owner and commiter of JacpFX (http://jacpfx.org); a framework to create Rich Clients in MVC style with JavaFX 8 and an Actor like component approach. Andy has posted 7 posts at DZone. You can read more from them at their website. View Full User Profile

Use JavaFX2 with Spring

04.15.2012
| 8252 views |
  • submit to reddit

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.

 

 

 

Published at DZone with permission of its author, Andy Moncsek. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Greg Brown replied on Mon, 2012/04/16 - 7:17am

Interesting example. In case you were not aware, you could do the same thing in FXML a bit more succinctly:

<?xml version="1.0" encoding="UTF-8"?>
<?import org.jacp.*?>
<RootComponent>
    <JavaFXSpring.LeafOne/>
    <JavaFXSpring.LeafTwo/>
</RootComponent>

The code to load the markup would look like this:

package examples.fxml;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class FXMLExample extends Application {
   @Override
   public void start(Stage stage) throws Exception {
       stage.setScene((Scene)FXMLLoader.load(getClass().getResource("fxml_example.fxml")));
       stage.show();
   }
}

Of course, in FXML you don't need to create subclasses in order to use the platform components - you can just use them directly in your markup:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control?>
<?import javafx.scene.layout?>

<SplitPane>
    <GridPane>
        <Pane GridPane.hgrow="always" GridPane.vgrow="always">
            <Label text="left"/>
        </Pane>
    </GridPane>
    <GridPane>
        <Pane GridPane.hgrow="always" GridPane.vgrow="always">
            <Label text="right"/>
        </Pane>
    </GridPane>
</SplitPane>

Greg

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.