开发者

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 clo开发者_开发知识库ses 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜