Over a million developers have joined DZone.

Writeback Support in DataFX

· Java Zone

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

Over the last months, we have been working very hard on DataFX 2.0. We will talk about DataFX during JavaOne 2013, but I didn't want to wait to briefly mention one of the new cool features of DataFX 2.0, which is the write-back support.

With DataFX 1, you can easily obtain (remote) data and populate JavaFX controls with that data. Data can come from a variety of sources, in different format. In DataFX 2.0, it is also possible to modify data locally, and request that this data and then sent back to the original source. This is achieved by using a WriteBackHandler.

Both ObjectDataProvider and ListDataProvider now implement WriteBackProvider. As a consequence, when the data is changed locally after it has been retrieved (from a database, REST server, file,...) a callback will be executed in order to change the data on the source of origin. The developer is still responsible for doing this, by registering a callback handler with the DataProvider implementation.

You can checkout the code at https://bitbucket.org/datafx/datafx or you can download DataFX from maven central (groupId: org.javafxdata, artifactId: datafx-core and datafx-samples).

The code snippet below is taken form the JDBCSample.java file in datafx-samples, and it shows how to enable write-back support for a simple case where the data is stored in a relational database

DataReader dr = new JdbcSource(dbURL, converter, "PERSON", "firstName", "lastName", "country");
ListDataProvider lodp = new ListDataProvider(dr);
ObservableList myList = FXCollections.observableArrayList();
lodp.setWriteBackHandler(new WriteBackHandler() {
  public WritableDataReader createDataSource(Person me) {
  String statement = "UPDATE PERSON SET lastName=\'" + me.getLastName() + "\' WHERE firstName=\'" + me.getFirstName() + "\'";
  JdbcSource dr = new JdbcSource(dbURL, statement, null);
  System.out.println("Writeback called with statement "+statement);
  return dr;

DataFX will keep track of changes to properties in the retrieved data, and it will call the WriteBackHandler in order to make that change persistent.

speakingWe will provide more samples and tutorials later, and you are very welcome to join us at JavaOne (CON3202, Wednesday Sep 25, 08:30 am, Hilton - Plaza B)

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Published at DZone with permission of Johan Vos, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}