How to Present Spring Data Neo4j Nodes in a JavaFX TableView
How to Present Spring Data Neo4j Nodes in a JavaFX TableView
Join the DZone community and get the full member experience.Join For Free
RavenDB vs MongoDB: Which is Better? This White Paper compares the two leading NoSQL Document Databases on 9 features to find out which is the best solution for your next project.
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?
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?
- CustomerApp is the main class and starts the application by setting the context and the JavaFX screen.
- CustomerAppConfiguration loads the ScreensConfiguration, which sets up the JavaFX application and its beans, and the Neo4jConfig, which sets up the Neo4j database and starts the DatabaseSetup .
- DatabaseSetup autowires the neo4jCustomerRepository and if the repository is empty, it creates some nodes and some relations between the nodes.
- Neo4jCustomerRepository is a repository for Neo4jCustomer nodes and contains the search method.
- Neo4jCustomer implements the Neo4jNode interface and contains all customer properties and their methods.
- MainController is the StageController for the main window and holds the connection to the Main.fxml, which contains the JavaFX interface elements for the main stage.
- FXMLStage and FXMLDialog are helping to connect the fxml files with the Spring beans.
- StandardController manages the dialogs (up to now there is only one).
- Neo4jTableBuilder provides the connection between Spring Data Neo4j and JavaFX.
- Neo4jTableBuilderColumnField and Neo4jTableBuilderColumnSetMethod are the annotation classes that are used in the Neo4j node class to mark the fields and methods, that shall be presented in the JavaFX TableView.
How does the Neo4jTableBuilder work?
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?
If you have any feedback, please write to Elisabeth.Engel@comsysto.com!
Published at DZone with permission of Comsysto Gmbh , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.