Search code examples
javaswinguser-interfacewhile-loopsecondaryloop

"GUI while loop"


Okay, I have a code that looks like this:

public class Test
{   

private JPanel dummy;

public checker()
{
    dummy = new JPanel();
    dummy.setVisible(false);

    dummy.addComponentListener(new ComponentAdapter()
    {
        @Override
        public void componentShown(ComponentEvent arg0)
        {
            dummy.setVisible(false);
            runCheck();
        }           
    });

    runCheck();
}


private void runCheck()
{
    if (a)
    {
        //do something
        dummy.setVisible(true);
    }
}

}

This will create a dummy JPanel and add a component adapter that will fire each time dummy is set to be visible. It works like a while loop, only it makes sure that GUI is updated before it goes into another cycle.

But I need method checker() to return a value once the cycle is broken.

NOTE: This is symplified version of code, I cannot check for condition a, that is not a solution.


Solution

  • I figured it out. I can use secondaryLoop. I am unable to explain how it actually works, but it enables me to freeze the program (but the EDT) and, while the code is frozen, add events to the EDT queue which are done immediatley (if EDT isn't busy doing anything else, which, in my case, isn't).

    More on topic: link