Home>

Write in front

During the development of javafx programs,When using multithreading,When the program exits by default,Newly opened threads are still running in the background.

under these circumstances,Can listen for window close events,Close child threads inside.

2. Specific implementation examples

package sample;
import javafx.application.application;
import javafx.beans.value.changelistener;
import javafx.beans.value.observablevalue;
import javafx.event.eventhandler;
import javafx.fxml.fxmlloader;
import javafx.scene.parent;
import javafx.scene.scene;
import javafx.stage.stage;
import javafx.stage.windowevent;
public class main extends application {
  @override
  public void start (stage primarystage) throws exception {
    parent root=fxmlloader.load (getclass (). getresource ("sample.fxml"));
    primarystage.settitle ("hello world");
    primarystage.setscene (new scene (root, 300, 275));
    primarystage.show ();
    primarystage.setoncloserequest (new eventhandler<windowevent>() {
      @override
      public void handle (windowevent event) {
        system.out.print ("Listen to window close");
      }
    });
  }
  public static void main (string [] args) {
    launch (args);
  }
}

Among them, this is the specific implementation of the specific listening window closing:

  primarystage.setoncloserequest (new eventhandler<windowevent>() {
      @override
      public void handle (windowevent event) {
        system.out.print ("Listen to window close");
      }
    });

3. Effect

When clicking the window close button,The console will output

  • Previous Example of dynamic progress bar function implemented by bootstrap + jQuery
  • Next AngularJS example of reading JSON and XML files