Pick up Frame already started from the "Event Dispatch Thread"

Asked

Viewed 148 times

1

I have a desktop application on swing, and in it, I have a single JFrame which is always visible while the application is open. There are also some JDialogs modals dependent on this Frame, which open from certain buttons in the JFrame.

I’m testing a progress bar using SwingWorker(another question asked and answered here), and I need to pass the Frame for this class to activate the progressBar, but I need to get the Frame instance already opened in another different class.

In the Frame, I have a public method called getInstance(), that returns the window’s own instance, but to use it, I need to already have a Frame object created, but in this case, I cannot create another instance, I need to get the one that is already active.

From there, I wondered if maybe the EDT would allow, somehow, to recover the current open frame, even more because the classes where I need to make this call, were also initiated in the EDT, but I don’t know how it does it.

It is possible, from the (EDT), retrieve this Frame already initialized and visible in the application? If yes, how to do this?

1 answer

0


From of this answer that I found in Soen, the solution was to use the static method getFrames(), which returns an array of Frames created by the application. As this method can also return auxiliary frames created by JVM, within the loop I check if the Frame is an instance of my Frame main, and only then am I sure that the Frame captured is exactly my screen.

Frame[] frames = Frame.getFrames();
        for (Frame f : frames) {
            //ListaDeOficiosUI é o nome da minha tela que
            // estende JFrame
            if (f instanceof ListaDeOficiosUI) {
                this.instance = f;
                break;
            }
        }

Perhaps not such a suitable solution to use, but palliatively, solved my problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.