Dec 08
22
Using Java with JavaFX
Up until recently I have been playing around with JavaFX getting my hands dirty on some of the finer points of the FX Script language. But I recently realised that I have not really been using JavaFX with Java…but rather just JavaFX itself.
In real world development where timelines are important, money matters, and efficiency is strived for, the difference between a easy to use UI layer and a confusing, bloated, poorly implemented UI later can make or break a project.
I thought I would give a basic example of a seperated app using Java as the business/DAO layer, and JavaFX as the View layer. (i.e. a very basic MVC based app).
This app will make use of a little JavaFX application that I wrote the other night that displays bar graphs, which every business manager loves to look at right?
The example application will look like this: (click to enlarge)
Pretty simple graph, but the idea is that the data for the graph is powered by a Java back-end.
I’m using Netbeans 6.5 for this application example and have 2 projects, one Java project called EmployeeSales and one called JFXGraph.
The employee sales is really just an example application that contains 3 files,
- Employee.java
- EmployeeDao.java
- EmployeeDaoImpl.java
Here are the file listings:
Employee:
package employees.domain;/*** @author Mark Macumber*/public class Employee {private String name;private int age;
public Employee(String name){ this.name = name;}
public String getName() { return name;}
public void setName(String name) { this.name = name;}
public int getAge() { return age;}
public void setAge(int age) { this.age = age;}}
EmployeeDao:
package employees.dao;
import employees.domain.Employee;import java.util.List;
/*** @author Mark Macumber*/public interface EmployeeDao {List<Employee> getEmployees();
int getSalesForEmployee(Employee emp);}
EmployeeDaoImpl:
package employees.dao.impl;import employees.dao.EmployeeDao;import employees.domain.Employee;import java.util.ArrayList;import java.util.List;import java.util.Random;
/*** @author Mark Macumber*/public class EmployeeDaoImpl implements EmployeeDao{public List<Employee> getEmployees() { ArrayList<Employee> empList = new ArrayList<Employee>(); empList.add(new Employee("Mark")); empList.add(new Employee("Peter")); empList.add(new Employee("Luke")); empList.add(new Employee("Steve")); return empList;}
public int getSalesForEmployee(Employee emp) { Random random = new Random(); return random.nextInt(450);}}
As you can see, its just a basic sample implementation, but I’m sure you can imagine how it could be translated into a real world application business/DAO layer.
Now for the fun part, the integration…
The usage for the JFXGraph application I have made is quite simple:
var graph = BarGraph{ xPosition: 150, yPosition: 500 columns: bind barGraphColumns;};
You just need a sequence of BarGraphColumn’s each with a “tag” and “value” set.
So using the EmployeeDao to retrieve a collection of Employee objects in the main.fx file is quite simple, and the listing follows:
var employeeDao = EmployeeDaoImpl{};var employees = employeeDao.getEmployees();var columns:BarGraphColumn[];
for(emp:Employee in employees){ var col = BarGraphColumn{ tag: emp.getName(); value: employeeDao.getSalesForEmployee(emp); }; insert col into columns;}
var graph = BarGraph{ xPosition: 150, yPosition: 500 columns: bind columns;};
Stage { title: "JavaFX Graph example application" width: 800 height: 600 scene: Scene { content: [ graph ]; }}
As you can see, I create a new instance of the EmployeeDaoImpl and iterate over the results to create my sequence of BarGraphColumns, then just attach them to the graph, and setup the Stage.
That’s it.
With this in mind its easy to see just how easy implementing a view layer into any application can be with Java and JavaFX both cooperating together (as they should).
Below is a link to launch the Graph Application, just for good measure!

