NoSQL Zone is brought to you in partnership with:

Java Geek and managing director@comSysto GmbH in Munich ... Spring over JavaEE, Apache Wicket over JSF, Gradle over Maven, Lean over Waterfall, exploring JavaFX, Highcharts, Android, AgileUX, Lean Startup. Daniel is a DZone MVB and is not an employee of DZone and has posted 41 posts at DZone. You can read more from them at their website. View Full User Profile

How to Present Spring Data Neo4j Nodes in a JavaFX TableView

02.23.2013
| 3150 views |
  • submit to reddit

Today I would like to show you how to create a JavaFX TableView that displays Spring Data Neo4j nodes. Additionally the user can add new nodes, search for existing ones and edit them.

Why?

JavaFX is a aspiring new technology to build user interfaces and Neo4j is a graph database, which allows to insert a massive amount of nodes and search them in nearly constant time. But up to now, there is no possibility to present the Neo4j nodes in a JavaFX TableView.

So how will it look like in the end?

Spring Data Neo4j JavaFX TableView Demo

How to install the demo on your computer?

The source code of this demo is available on GitHub: https://github.com/comsysto/spring-data-neo4j-javafx-tableview. To execute it, build it with maven and start the CustomerApp class.

What are the components?

How does the Neo4jTableBuilder work?

To construct a Neo4jTableBuilder, you need the node class which implements Neo4jNode (in our case the Neo4jCustomer class), the repository instance of these nodes and the JavaFX TableView instance.

Afterwards, you can initialize the data table by calling initDataTable(). This will set up the columns of the table according to the annotations in the node class.

For each field that is annotated with @Neo4jTableBuilderColumnField(columnName = “xy”, columnOrder = 42) a column is created with the given column name. In the end, all columns will be sorted by ascending column order values. If the field should not be editable, you can declare it readonly by stating columnType  = Neo4jTableBuilderColumnField.FieldType.readOnly.  As default, fields are edittable and have a columnOrder of 10. Fields can be of the following types: String, Integer and Date. If you need other types, you can include them with their StringConverter in the createAndSetFactories() method.

For all set-methods in the Neo4j node class which are annotated with @Neo4jTableBuilderColumnSetMethod(columnName = “xy”, columnOrder = 42) the Neo4jTableBuilder creates a editable column. Property type, column name and order work according to the field annotation.

So when should you annotate the field and when the set-method? By default, it is sufficient to annotate the field. Only if side effects should happen while saving values, a set-method is needed. In our case, the name property shall be updated, if the first or last name changes. Therefore, the set-methods of first and last name are annotated instead of their fields. Notice that the field and the set-method need corresponding names, e.g. the field for setFirstname(String firstname) must be String firstname. Otherwise, the Neo4jTableBuilder can’t read the values.

Finally we call setTableData(neo4jCustomerRepository.findAll()) to initially present all nodes.

How does the search function work?

When the user enters text in the JavaFX text field or changes an exiting search term, the searchTextfieldKeyReleased() method is called and resets the table data to all nodes that contain the search term in their name.

How to add new nodes?

The first row of the table is marked in green and represents a new node, that will be saved in the repository as the user clicks on the Save button. Afterwards, a new node is created, that can be saved as well.

How to edit existing nodes?

To edit an existing editable node, just mark the row by clicking on it and then click an the field you want to edit. The field turns into an input field and by clicking somewhere else the change will be saved, as long as it matches the field format. If not, the field will be reset to the previous value. This functionality is implemented in the inner class EditingCell. So far, pressing the return key doesn’t end the editting state.

How to delete nodes?

Nodes can be deleted by clicking on the red X button in the last column. You can find the implementation in the inner class ButtonTableCell.

How does the column sorting work?

By clicking on a column heading, the table entries are sorted in ascending order. If you click again, the sorting order is removed. Up to now, columns can’t be sorted in descending order and changing columns by pressing the tab key doesn’t work correctly.

What may help if you have problems?

If you get a java.lang.RuntimeException: Exception in Application start method Caused by: org.springframework.beans.factory.BeanCreationException you probably started the application twice. Stop both instances and try again.

If miracles occur, it may help to delete the data folder and try again.

How can this demo be extended?

Of course, you can try to overcome the limitations mentioned above. Moreover, you can implement ways to represent relations and manage them. It’s also possible to add more tables with a Neo4jTableBuilder for each of them. Additionally, you can create a login and restrict opperations as Stephen Chin showed in Steve On Java.

What is the demo based on?

This demo is based on Steve On Java – JavaFX , JavaFX Documentation Home – TableView and Neo4j HelloWorlds Example.

If you have any feedback, please write to Elisabeth.Engel@comsysto.com!

 

Published at DZone with permission of Daniel Bartl, author and DZone MVB. (source)

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