Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
150 views
Welcome To Ask or Share your Answers For Others

1 Answer

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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...