calling invokeAndWait from the EDT

后端 未结 4 1998
忘了有多久
忘了有多久 2020-12-16 22:34

I have a problem following from my previous problem. I also have the code SwingUtillities.invokeAndWait somewhere else in the code base, but when I remove this

4条回答
  •  时光取名叫无心
    2020-12-16 23:19

    Based on the comments, it appears you are not repainting the frame once the actions are completed. If you do not, then the screen will only be updated at what seems to be random times (when another window moves in front, perhaps).

    Inside gui.update, I suggest you make the last line:

    myFrame.repaint();
    

    (more or less, depending on your circumstances).


    Edit: As it turns out, the actual problem is this loop:

    synchronized(performedAction){
        while(!hasPerformedAction()){
            try {
                performedAction.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        setPerformedAction(false);
    }
    

    Since there is only one application thread (which happens to be the EDT), the result of hasPerformedAction() can never change (assuming it's a simple getter). There is no other thread to change the value. Since this infinite loop is on the EDT, the GUI can never be repainted; hence it locks up.

提交回复
热议问题