Macca Blog

My life experiences with code and design

Using Java with JavaFX

Posted on by Mark

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,


Here are the file listings:

package employees.domain;
* @author Mark Macumber
public class Employee {
private String name;
private int age;

public Employee(String name){ = name;

public String getName() {
return name;

public void setName(String name) { = name;

public int getAge() {
return age;

public void setAge(int age) {
this.age = age;


package employees.dao;

import employees.domain.Employee;
import java.util.List;

* @author Mark Macumber
public interface EmployeeDao {
List<Employee> getEmployees();

int getSalesForEmployee(Employee emp);


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!

This entry was posted in Uncategorized. Bookmark the permalink.

8 Responses to Using Java with JavaFX

  1. emaillenin

    Hey nice post! to explain how to use java as the backend and javafx as the front end. I was searching google, to know how to use List in JavaFx and finally ended up in your blog. my query is solved with this post.

  2. Mark

    Thanks very much, glad to hear it helped you out.

  3. MedSabri

    It’s a very nice post, thx a lot, but can u explain the position of the main.fx in the project architecture.
    or the zip file of this example.

  4. Mark

    Hello MedSabri,

    The main.fx file is simply the “starting” file that will initialise and run the projec, no real architectural reason why it is placed where it is. There just needs to be a main class that the app needs to run from.

  5. MedSabri

    Thank you very much for your response, but my propose is the second project called JFXGraph, i’m trying to run your example but unsuccesfully.

    Thanks for your help.

  6. Mark

    I see, you are trying to run the example code on the JFXGraph code sample page? I will post on that page for the resolution…

  7. Ben

    Thanks for great information.
    Web design service

  8. pbsl

    you have a nice site.thanks for sharing this site. various kinds of ebooks are available here

Leave a Comment

Your email address will not be published. Required fields are marked *