4

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?

1

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

The class with the JFrame doesn't know about closing the resource, because the closing of this JFrame shall trigger the closing of the resource. Stefan (sry about writing my question so ambiguous)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.