3

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Henry
  • 1,395
  • 1
  • 12
  • 31
  • 1
    Possible duplicate of [How to close a java window with a button click - JavaFX Project](http://stackoverflow.com/questions/25037724/how-to-close-a-java-window-with-a-button-click-javafx-project) – António Ribeiro Mar 12 '16 at 21:07
  • @aribeiro That looks like a different question entirely. – James_D Mar 12 '16 at 21:32
  • You can add listener with: primaryStage.setOnCloseRequest((event) -> { closeConnection() }); – xoned Mar 13 '16 at 18:17

1 Answers1

3

override onStop- (got me thinking in android for a second) in your Application class

 @Override
 public void stop() throws Exception {  
//  clean up goes here   
   //super.stop(); reason for comment this line, @fabian's comment
}
Elltz
  • 10,730
  • 4
  • 31
  • 59
  • I did some further digging after posting my question and learned that I could override the stop() method and put my own cleanup code in there. I wrote that code but then I realized I didn't need it. Java was already closing the database connection by default. (I ran DB2 List Applications commands to verify that.) I didn't realize Java was that smart :-) I've commented out the overridden stop() method and the method I wrote to close the connection but can always uncomment them if Java's default behaviour changes.Thanks for the prompt reply! – Henry Mar 12 '16 at 22:02
  • 1
    There is no need to call `super.stop()` since that method "does nothing" (see https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html#stop--). (The method body is empty). – fabian Mar 12 '16 at 23:57