Get Frame already started from the "Event Dispatch Thread"

2

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

I'm testing a progress bar using JFrame (another question asked and answered here ), and I need pass the Frame to this class in order to enable SwingWorker , however I need to get the Frame instance already open in another class.

In progressBar , I have a public method called Frame , which returns the window instance itself, but to use it, I need to already have a Frame object created, but in this case, I can not create another instance , I need to get the one that is already active.

From there, I thought if EDT might somehow allow me to retrieve the current Open Frame, even more so because the classes where I need to make this call, were also started in WBS, but I do not know how to do it .

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

    
asked by anonymous 05.04.2016 / 17:47

1 answer

1

From this answer I found in SOEn, the solution was to use the static getFrames() , which returns an array of Frames created by the application. As this method can also return Auxiliary Frames created by the JVM, inside the loop I check if the Frame is an instance of my parent Frame , and only so I am 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;
            }
        }

It may not be such an appropriate solution to use, but palliatively, it solved my problem.

    
06.04.2016 / 16:48