As far as I know, there's no way in Java to catch the system shutdown or logoff events.
You can, however, catch when the JVM is terminating by adding a shutdown hook.
AWT's WindowAdapter also has a windowClosing event that you can override and hook to a window that you want to monitor. Swing inherits this; I believe SWT does as well. Be aware that you MUST manually dispose of the window if you override this event!
I believe that MS Windows will fire these events as it is closing. I believe a SIGTERM on Linux/UNIX does the same, although Linux will SIGKILL an app shortly afterwords if this is during shutdown.