-1

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.

  • When you dispose of your frame, should you also delete it from the `frames` array to avoid visiting it again? – Ole V.V. Jul 05 '16 at 14:19
  • 2
    The description makes this app. sound like a horrid experience for the end user. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) In this case, the frame should probably be a dialog and instead of having a number of them, have just 1, keep a reference to it, and show it when and with whatever text it needs. – Andrew Thompson Jul 05 '16 at 14:19
  • The program called by `callMyFrame()` is not mine, so I can not change anything about it, it have to be many frames (which are disposed of on each loop). – alexmagnus Jul 05 '16 at 14:33
  • *"so I can not change anything about it,"* ..are you familiar with the terms 'inheritance' & 'override'? – Andrew Thompson Jul 06 '16 at 12:23
  • BTW - the class name `MyFrameType`, which includes `My` rather than a useful name, suggests it is either coded by you, or another newbie. If it's written by you, then you ***can*** change it. If it is written by another newbie, then *you **shouldn't** be using it.* Newbies write terrible code, usually not fit for human sight, let alone distribution to other newbies. So color me 'unconvinced' of your claim, quoted in my previous comment .. And a tip: Tip: Add @OleV.V. (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Jul 06 '16 at 12:28
  • @Andrew Thompson, MyFrameType is the name I use here, in the actual application it has a different name. As for overriding the methods of the original program, I could do it, but it would hardly change much in the user experience. In the program (which I write for my thesis - the original program calling the frame was written by some previous student and its usage recommended by the supervisor), windows of all kinds keep popping up and disappearing, with many of them being forced to be opened (currently discussing with my supervisor if there is any possibility to rewrite that part). – alexmagnus Jul 07 '16 at 13:21
  • *"`MyFrameType` is the name I use here.."* It's an extremely unhelpful and misleading (and rather unimganative) name. The actual name should be used, but failing that, something more descriptive like `ThirdPartyResultsFrame` would be better.. – Andrew Thompson Jul 07 '16 at 13:38

2 Answers2

0

You can just check if the JFrame is displayable, here is a simple example that you can use to verify:

 JFrame jFrame = new JFrame();
 jFrame.setVisible(true);
 jFrame.dispose();
 System.out.println(jFrame.isDisplayable());

Output:

false

So in your loop instead of just checking if the JFrame is an instanceof MyFrameType you can do:

for (Frame openFrame : frames) {
    if (openFrame instanceof MyFrameType && openFrame.isDisplayable()) {
    }
}
explv
  • 2,709
  • 10
  • 17
0

You should set the frame in the array to null, or otherwise indicate that it is "deleted". Also, to do this you need to use a standard for-loop instead of the enhanced for-loop, something like this:

    Frame[] frames = Frame.getFrames();
    for(int i=0; i<frames.length; i++) {
        if (frames[i] instanceof MyFrameType) {
            (do something, read the frame etc.)
            frames[i].setVisible(false);
            frames[i].dispose();
            frames[i].dispatchEvent(new WindowEvent(
                    frames[i],
                    WindowEvent.WINDOW_CLOSED));
            frames[i] = null;
           break;
        }
    }
Magnus
  • 17,157
  • 19
  • 104
  • 189