Java Swing Application Lifecycle Issue

I have a Java application that is used to run the Swing user interface. An interface is a class with an encapsulated JFrame instance. The problem is that the application allocates some resources, and the Swing interface uses these resources, but the application closes the resource, not the user interface.

How can I achieve either the main application receives a notification when the full Swing interface is closed, or that the beginning of the Swing interface is blocked until it is closed. Closed means that the WindowAdapter.windowClosed(WindowEvent) method for the JFrame WindowListener has already been called.

The solution to this topic ( link ) seems to be back when the JFrame is invisible, does this include WINDOW_CLOSED event WINDOW_CLOSED ?

Edit: Perhaps this will be the solution to implement this lifecycle interface:

 public interface Lifecycle { public void startup(); public void shutdown(); } 

Now the Swing interface class should call the shutdown() method of the main application in the WindowEvent.WINDOW_CLOSED event handler .

Is this a good mark possible?

+4
source share
2 answers

Try using Toolkit.getDefaultToolkit().addAWTEventListener() . If you provide the appropriate event mask, you can get the events you need. This does not subscribe to a specific instance of JFrame.

+2
source

The approach should be as follows:

  • Resource closure should trigger an event
  • A class encapsulated by a JFrame must listen for a resource close event
  • and the listener should call the JFrame.setEnabled (false) method to disable it
0
source

All Articles