I'm new to JavaFX (and rather rusty with Java in general) and I need to do some cleanup tasks in a JavaFX application.
My application displays an input form which writes each form's worth of data to a DB2 database as a separate row. When the program begins, it acquires a database connection first and doesn't even bother to draw the GUI until it has succeeded; if the database is down, there's no point in gather data to be added to the database. Assuming the connection has been successfully acquired, the application runs for the entire day shift and the code simply reuses the original connection all day. Each time the form is completed, the user clicks the Add button and the same Statement is reused to create an Insert statement and then commit the Insert. So far so good.
My question involves when/how to close the database connection. Logically, I think I want to do it when the user clicks on the Close button on the application. At that point, I'll execute my disconnectFromDatabase()
method and let the application close gracefully.
I'm having a hard time trying to figure out when/how to detect that the Close button has been pressed. I used to use Swing and use the setDefaultOnClose
to detect that the user was closing the app but I'm not finding an equivalent in Java FX.
Where/how should I detect that the Close button has been pressed by the user? I'm guessing I need to write a listener to detect that action but I'm really not sure how to go about it.
I can post the program as it stands if necessary but if you can describe what I need to do in a few sentences, I can probably figure out the precise code I need on my own.
Update
Thanks for suggesting the other post but it didn't really answer my question. I was looking for a way to determine that the Close button on the GUI had been pressed so that I could add my own cleanup code that should be executed in addition to the standard things that Java does. As it turns out, Java was already closing my database connection as part of its default behaviour so I didn't need to write any code after all. However, I know that overriding the stop()
method seems to be a good way to go if I ever need to do cleanup that isn't done by Java automatically.
Update
I've now resolved the issue.