For a little while now, I’ve been working on an application that manages a list of documents, providing multiple views that the user can edit.
The application looks something like this:
The user selects the document they wish to view or edit by selecting it from the large
TableView in the middle of the window. The area on the right provides controls to view and edit details. (The area on the left is for filtering the documents displayed in the central table.)
Based on some early advice, I had watchers on the focus property of the fields that could be edited. When a control lost focus, any changes were written to the database. The user didn’t have to do anything to save their work. It just happened.
This worked with Java 7 and JavaFX 2. After the switch to Java 8 and JavaFX 8, things were not quite the same. If a user was making a change somewhere and then selected another document without moving to another editing view, the data was lost. The focus change notification did not arrive before the new document was selected in the table (repopulating the editing control before the data was saved.)
I posted a question about this, along with a SSCCE on StackOverflow. The gist of the few answers I received was that I was doing the updates wrong. Kleopatra informed me that I was probably doing the update wrong since there is no guarantee as to the order of various events and notifications. I had already tried doing the update during the document selection process of the
TableView as suggested by eckig, but doing that left the application vulnerable to lost data if the user did an edit and just closed the program.
In the end, I did use a listener on the change of selection in the table and did an override of the
stop() method of the
Application class to catch needed updates before closing.
But the key was creating a new class
MonitoredSimpleStringProperty, derived from
SimpleStringProperty, that keeps track of whether it’s contents have been altered. With that information, the application can decide if it even needs to update the database as the user selects a different document to view or edit.
Another, longer SSCCE illustrates how it works:
The actual program is a bit more complicated, but this approach solved my problem. Now on to other things.
I started working on this program as a result of something else I was trying to do. I’ve been working on a book – “Molecular Dynamics Simulation for Beginners”. As part of writing that, I need to maintain a list of references to be included for the reader that wants to dig deeper into some areas. The book is being written in LaTeX because of the math involved. Traditionally, LaTeX documents are handled by a program called BibTeX or something similar. There are lots of reference managers. Many are free. Many are open-source. None of them did exactly what I want. So… down the rabbit hole.