I am making a desktop application which has login and logout functionality with server.
I need to logout from the application whenever someone close the window, so I am using these codes
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
event.consume();
closeWindow();
}
});
where closeWindow() contains logout and other related steps.
Now there is a problem when application closes unexpectedly or when someone forcefully quit/close it from Task Manager (by ending process).
Do JavaFX has any event to capture forcefully quit or unexpected close ? Or if there any method to stop it ?
When your application gets closed via TaskManager
your only option will be to use the VM's shutdown hook.
There are some examples around, e.g. here.
There is a method
@Override
public void stop() throws Exception {
super.stop();
System.out.println("logout");
}
in Application class.
public class Gui extends Application {
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
closeWindow();
}
});
}
@Override
public final void start(Stage primaryStage) throws Exception {
// ...
}
@Override
public void stop() throws Exception {
// ...
System.exit(0);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With