I have a following situation: my program repeatedly calls something that makes a JFrame with text appear. I read the text from that frame. The frame has a special type, let's call it MyFrameType, so I recognize it by the type, and dispose of the frame at the end of each call, in all possible way of closing a frame I know of... (the following code is within a loop)
callMyFrame();
Frame[] frames = Frame.getFrames();
for (Frame openFrame : frames) {
if (openFrame instanceof MyFrameType) {
MyFrameType myFrame = (MyFrameType) openFrame;
(do something, read the frame etc.)
myFrame.setVisible(false);
myFrame.dispose();
myFrame.dispatchEvent(new WindowEvent(
myFrame,
WindowEvent.WINDOW_CLOSED));
break;
}
}
Now, the problem is: while the frame gets actually closed (which is not always case with the last frame for some reason - sometimes it gets closed, sometimes not), the closed frames are still listed by the frames array and the program ends up reading from the wrong frame - the first one of MyFrameType it finds, although only one of them is actually open. I know that getFrames()
gets all frames created by the application. But how do I exclude those disposed frames, so that it doesn't just pick a random previously created, long forgotten frame? The frames in question have apparently no owner, so that removing ownerless frames does not do the job.