A JavaFX CRUD (Create, Read, Update, Delete) GUI (Graphical User Interface) application.
This application provides CRUD capability utilizing the public JSON Placeholder API and displays the results in a JavaFx application. The JSON Placeholder post resource is used to invoke the APIs via Java's HttpClient introduced in Java 11.
To run the application enter the below from a terminal window.
mvnw javafx:run (use ./mvnw for Unix/Linux based OSes)
Searches are performed by numeric id or by the default which will retrieve all post objects when clicking on the Search button when All is selected from the Search By drop down list.
All the table columns are sortable by clicking their respective column headers. Continuously clicking the column header cycles the sort direction. Note the upside down triangle icon in the Id column below.
The Delete and Create buttons are only enabled if a table row is selected.
The JSON Placeholder API's create (POST), update (PUT) and delete (DELETE) operations are faked and will not persist changes on the server side. The returned responses will reflect results as if the operations were actually persisted server side.
The only editable columns in the result table are the Title and Body columns. To initiate an edit, double click inside those column values on the selected row. Double clicking the column value will change the column into an editable text field. Make the desired change and press enter to execute the PUT API and persist the change in the table's column.
Clicking on the Create button will display a data entry dialog box for creating a new post. The created post will be associated with the user id value of the currently selected row. The dialog's User Id text field will be pre-filled and disabled.
Since creating a new post is faked by the JSON Placeholder API, creating multiple posts produces the same post id for each newly created post. This isn't representative of an actual API call and as such sorting the result table produces anomalous results on those newly created rows with the same id. I believe this is related to the table's backing ObservableList interface and the unique id assigned to each Post object via the class hashCode method. Since the same id (and subsequently same hash code) is generated for new posts this is most likely causing the anomalous results.
Another issue uncovered with the newly created posts occurs when editing the editable column (Title and Body) values. Editing those column values produces a 500 Internal Server Error response code when calling the PUT API. This issue is listed in the JSON Placeholder API's known issues and I was also able to reproduce the error when using Postman to execute the PUT API with a post id value of 101 or greater. The below stack trace is produced in the Postman console window which is the same error reported in known issue 98.
TypeError: Cannot read properties of undefined (reading 'id') at update (/app/node_modules/json-server/lib/server/router/plural.js:262:24) at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) at next (/app/node_modules/express/lib/router/route.js:137:13) at next (/app/node_modules/express/lib/router/route.js:131:14) at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) at /app/node_modules/express/lib/router/index.js:281:22 at param (/app/node_modules/express/lib/router/index.js:354:14) at param (/app/node_modules/express/lib/router/index.js:365:14) at Function.process_params (/app/node_modules/express/lib/router/index.js:410:3)
Despite the issues stemming from using the free, fake JSON Placeholder API, this application is illustrative of using JavaFx as your GUI.
Run the below command from a terminal window in the application's root folder to build the application.
mvnw package (use ./mvnw for MacOS/Linux based OSes)
The command will create the following two JAR files in the project's root folder target directory.
- javafx-example-0.0.1-SNAPSHOT.jar
- javafx-example-0.0.1-SNAPSHOT-jar-with-dependencies.jar
The javafx-example-0.0.1-SNAPSHOT-jar-with-dependencies.jar file is an executable JAR that includes all the project's dependencies.
Since Java 11, JavaFX has been unbundled from the standard Java Development Kit (JDK). Therefore, running the JavaFX application via the executable JAR requires external dependencies or a JavaFX-integrated JVM. To download a JavaFX-integrated JVM you can use SDKMAN!. JavaFX-integrated JVMs will contain fx as part of the version and identifier. Once a JavaFX-integrated JVM is installed, type the following from the terminal window to execute the application.
java -jar javafx-example-0.0.1-SNAPSHOT-jar-with-dependencies.jar
If not using a JavaFX-integrated JVM you will need to download the JavaFX external dependencies which includes platform-specific native binaries. After downloading and extracting the platform-specific SDK ZIP file, type the following from the terminal window to execute the application.
java --module-path PATH_TO_EXTRACTED_FX_LIB_FOLDER --add-modules javafx.controls,javafx.fxml -jar javafx-example-0.0.1-SNAPSHOT-jar-with-dependencies.jar
where PATH_TO_EXTRACTED_FX_LIB_FOLDER is the lib folder location of the extracted ZIP file's contents. For example:
java --module-path $HOME/javafx-sdk-23.0.1/lib --add-modules javafx.controls,javafx.fxml -jar javafx-example-0.0.1-SNAPSHOT-jar-with-dependencies.jar





