How to Present Spring Data Neo4j Nodes in a JavaFX TableView
Join the DZone community and get the full member experience.Join For Free
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.
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?
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 email@example.com !
Published at DZone with permission of Comsysto Gmbh, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
How to Optimize CPU Performance Through Isolation and System Tuning
Mainframe Development for the "No Mainframe" Generation
DevOps Midwest: A Community Event Full of DevSecOps Best Practices
HashMap Performance Improvements in Java 8