-1

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
  • I don't think this is a good pattern. Why don't you just perform your loop on a background thread? – Vincent van der Weele Aug 04 '13 at 20:37
  • make `a` static and create a listener for it. Once `a` is false, the listener fires `checker` – bas Aug 04 '13 at 20:38
  • 1
    I'm going to double down on what @Heuster said -- this does not at first glance appear to be a sane pattern. If you could at least provide a little better description of what you're trying to achieve a solution sample could probably be provided. – lscoughlin Aug 04 '13 at 20:48
  • what is `a`? try using `PropertyChangeListener` or something more simple like that – nachokk Aug 04 '13 at 21:01

2 Answers2

3

Use SwingWorker to perform the runCheck() loop in the background. Invoke

dummy.setVisible(false);
worker.execute();

You can call setVisible(true) in done(), as shown here for setEnabled().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

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

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94