Delay only 1 class method, not whole program

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Delay only 1 class method, not whole program

bchinfosieeuw
How do I start the behavior of a certain method (possibly in another class) later than another method? I do not want to let the whole program sleep. Code I have now:

static Timer timer_1;
public void actionPerformed(ActionEvent e ) {

        if (e.getSource()==go){
                if (!timer.isRunning()) {
                        timer.start();
                       
                }
       
        //timer_1 = new Timer(0, new TimerAction1());
        TimerAction1 ta1 = new TimerAction1();
        ta1.fen();
        //timer_1 = new Timer(0, ta1);
        }
       
}

static class TimerAction1 implements ActionListener
{
        TimerAction1(){
        }
        public void actionPerformed(ActionEvent e){
        }
 

        void fen() {
       
        System.out.println("Fen method");
        }
}

I need to use the swing timer probably, but I do not know how to do so.
Reply | Threaded
Open this post in threaded view
|

Re: Delay only 1 class method, not whole program

gouessej
Administrator
What's wrong exactly? Yes you can use a Swing Timer, you can pass a delay to the constructor.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Delay only 1 class method, not whole program

bchinfosieeuw
I already figured it out. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Delay only 1 class method, not whole program

gouessej
Administrator
You're welcome.
Julien Gouesse | Personal blog | Website