I have a programm with a TextArea which enables the user to add notes to a particular verse number. Every verse number has a dedicated note entry. The values for the verses and the notes are loaded from a SQLite database. The text is loaded correctly and the "Save" ("Speichern") button is working.
I want to show an alert window, which displays the question "Do you want to save the text?", when the user has changed the TextArea and not clicked on the "Save" button. I have tried to add a listener for the TextArea having a variable , but the problem is, that it detects a change every time the Notes Text Area is updated.
My idea is to save the text of the Notes Text Area to a String or HashMap, after the verse is loaded. There should be a respective String or a HashMap which saves the original notes. The String / HashMap should then be compared to the value of the listener, but only if the "Save" button hasn't been clicked on before closing the programm.
How do I achieve this?
This is how the relevant of the programm looks like.
This is the method which I am trying to change:
private void loadNotesFromPaliVerse() throws SQLException {
// Check if a number has been selected
Integer selectedNumber = comboBoxVerseNr.getValue();
if (selectedNumber == null) {
return;
}
if (selectedNumber.equals(previousSelectedNumber)) {
return;
}
// Retrieve verse number based on comboBoxVerseNr
String queryNote = DatabaseMethods.notesToPaliVerse(comboBoxVerseNr.getValue());
try (PreparedStatement statement = connection.prepareStatement(queryNote)) {
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
// Retrieve text value from the table
String textValue = resultSet.getString("Notes");
// Insert text into TextArea
notesTextArea.setText(textValue);
} else {
// No corresponding entry found, clear the TextArea
notesTextArea.clear();
}
}
}
}