DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Use JavaFX2 with Spring

Use JavaFX2 with Spring

Andy Moncsek user avatar by
Andy Moncsek
·
Apr. 15, 12 · Interview
Like (0)
Save
Tweet
Share
14.79K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

 

 

Spring Framework

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices 101: Transactional Outbox and Inbox
  • Introduction to Spring Cloud Kubernetes
  • How We Solved an OOM Issue in TiDB with GOMEMLIMIT
  • Create Spider Chart With ReactJS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: