简体   繁体   中英

Java Swing application lifecycle problem

I have a Java application which is used to start a Swing user interface. The 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 gets a notification when the complete Swing interface is closed or that the start of the Swing interface blocks until it's closed. Closed means that the WindowAdapter.windowClosed(WindowEvent) method of the JFrame WindowListener was already invoked.

The solution from this thread ( link ) seems to return when the JFrame will be invisble, does this include the WINDOW_CLOSED event handling?

Edit: Maybe it will be a solution to implement this lifecycle interface:

public interface Lifecycle {

    public void startup();

    public void shutdown();

}

Now the Swing interface class has to invoke the shutdown() method of the main application in the handler of the WindowEvent.WINDOW_CLOSED event.

Is it possible and a good pratice to do so?

Try to use Toolkit.getDefaultToolkit().addAWTEventListener() . If you supply appropriate event mask you can get events you need. This is without subscribing to specific instance of JFrame.

The approach should be as follows

  • resource closing should raise an event
  • The class encapsulated the JFrame, should listen to the resource closing event
  • and the listener should call the JFrame.setEnabled(false) method to make it disable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM