Java: how to pause within actionPerformed

1

Good morning / afternoon / evening, lately I have had problems finding a solution for my problem what I try to do is pause a java program, this I used to do in C with sleep (milliseconds); and it worked as I wanted since the waiting time was done after the instruction that goes before of sleep but here in java I had problems I have dealt with Thread.sleep (milliseconds) and since I use it within actionPerformed, the waiting time is applied before executing the method, which I do not want. I would like to know if there is a way to apply the pause in the place I want, this is my code:

if(aux1.getIcon().toString().equals("f.jpg"))
    aux1.setIcon(img[id]);
else
    aux1.setIcon(new ImageIcon("f.jpg"));

if(turnos%2==0)
{
    // Justo aquí es donde quiero la pausa, que lo de abajo tarde en realizarse.

    if(aux1.getName().equals(aux2.getName()))
    {
        aux1.setVisible(false);
        aux2.setVisible(false);
    }
    else
    {
        aux1.setIcon(new ImageIcon("f.jpg"));
        aux2.setIcon(new ImageIcon("f.jpg"));
    }
}

I hope you can help me because I really can not find a solution for this problem I have.

    
asked by gustavo salgado 09.09.2017 в 05:47
source

1 answer

1

Java processes all mouse and keyboard events in a separate thread known as the Event Dispatch Thread (EDT). Calling Thread.sleep(milisegundos); prevents the UI from updating. I advise you to use a Swing Timer for what you want to do.

    
answered by 09.09.2017 в 08:47