Is it possible to be notified whenever any window in the application was created or closed?
At the moment I'm polling Window.getWindows()
but I would prefer to get notified instead.
What I have:
List<Window> previousWindows = new ArrayList<>();
while (true) {
List<Window> currentWindows = Arrays.asList(Window.getWindows());
for (Window window : currentWindows) {
if (!previousWindows.contains(window)) {
//window was created
}
}
for (Window window : previousWindows) {
if (!currentWindows.contains(window)) {
//window was closed
}
}
previousWindows = currentWindows;
Thread.sleep(1000);
}
What I'd like:
jvm.addWindowListener(this);
@Override
public void windowWasDisplayed(Window w) {
//window was created
}
@Override
public void windowWasClosed(Window w) {
//window was closed
}
JFrame
s are AWTWindows
, as are the other top-level Swing containers,JWindow
andJDialog
. – Greyback