Diese Seite (Version-1) wurde zuletzt am 28-März-2017 14:37 von Markus geändert.

Du bist nicht autorisiert, diese Seite umzubenennen.

Du bist nicht autorisiert, diese Seite zu löschen.

Versionsgeschichte der Seite

Version Zuletzt geändert Größe Autor Änderungen Kommentar

Links

Eingehende Links Ausgehende Links

Versionsunterschiede

Unterschiede zwischen Version und .

Zeile 1: 52 Zeilen hinzugefügt.
[{ALLOW view All}]
[{ALLOW edit Markus}]
!Shutdown a Java swing application
You find lots of examples in the web to close a swing application:
1. Add a default action like
{{{
yourJFrame.setDefaultCloseOperation(ClosableFrame.DISPOSE_ON_CLOSE);
}}}
2. Add a WindowListener, first windowClosing is called and thru "dispose" windowClosed is called
{{{
this.windowAdapter = new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
[...clean up code...]
yourJFrame.dispose();
}
@Override
public void windowClosed(WindowEvent e) {
}
};
}}}
3. just sig-kill everything with
{{{
System.exit(0);
}}}
This all works fine, but if the user shuts down the system or is logging off without closing, then the application will hang on Windows XP (and Windows 7).
I have to admit that I additionally use a javax.swing.Timer, but even without it hangs.
I tried several evenings and ended up in this solution:
1. Create a ShutdownHook
{{{
(class ShutDownHook extends Thread .... public void run() {...} )
ShutdownHook shutdownHook = new ShutdownHook();
Runtime.getRuntime().addShutdownHook(shutdownHook);
}}}
2. If the app is closed from the user, remove the shutdownHook (because it is called then also) and just stick on windowsClosing
3. If the system shutdowns, only the shutdownHook is called (not WindowClosing nor WindowClosed).
I did not find a way back to the JFrame queue (with dispose and all other kind of methods). So I cleaned everything up manually myself and hard-killed the app:
* stop my own Timer
* write user data to file
* System.runFinalization();
* Runtime.getRuntime().halt(0);
System.exit(0) is not enough here. The app still hangs. I was not able to trace it, because it is hard, when the system shuts down.\\
But anyway, this finally worked.