roesslerj
roesslerj

Reputation: 2661

How to get rid of "dead" windows in Java?

I am working with some legacy code. During execution, that code creates multiple windows and disposes them. However, they are still reachable and i.e. calling java.awt.Window.getWindows() returns a large array of java.awt.Window, javax.swing.JFrame, javax.swing.JDialog and the like, all of which have their name starting with "dead-". This now increasingly becomes a problem.

So here are my questions: Am I correct, that the 'dead-' prefix of the name is created by the System? What can I do to properly dispose old windows such that they are no longer reachable and returned by java.awt.Window.getWindows()? Thanks a lot!

Upvotes: 2

Views: 340

Answers (1)

roesslerj
roesslerj

Reputation: 2661

I found the following code inside the project:

private void disposeWindow(final java.awt.Window window) {
    SwingEnvironment.runOnEventDispatchingThread(new Runnable() {
        @Override
        public void run() {
            window.dispose();
            window.setVisible(false);
            window.removeNotify();

            String oldName = window.getName();
            if (oldName == null) {
                oldName = "unnamed";
            }

            if (!oldName.startsWith("dead-")) {
                window.setName("dead-" + oldName);
            }
        }
    });
}

So this is the reason the windows where named 'dead-'.

However they were properly disposed and still showed up in the Windows Array that is returned by the java.awt.Window.getWindows(). To get rid of them, I had to create a separate ThreadGroup and create a separate AppContext via SunToolkit.createNewAppContext();. Disposing that AppContext also disposed the Windows properly in my case. See also http://kingsfleet.blogspot.de/2009/10/how-to-have-more-than-one-instance-of.html.

Upvotes: 1

Related Questions