rahul bilove
rahul bilove

Reputation: 75

timer exit condition in java

                 String move=jTextField1.getText();             
                 i=Integer.parseInt(move);
                 timer = new Timer(1000,new ActionListener(){
                    public void actionPerformed(ActionEvent e)
                    {
                        i--;
                        if(i<=0)
                        {
                                if(move.equals("0"))
                                {
                                    Thread th=new Thread(new DetectImage());
                                    th.start();
                                   SwingUtilities.invokeLater(new Runnable(){
                            @Override
                            public void run() {
                                new TrafficMainGUI(storeValue);
                            }
                                   });

                                }

        timer.stop();
                        }
                        }
                        jTextField1.setText(""+i);
                    }
                 });
                    timer.start();

           move=""+i;



//Thread th in DetectImage class
 public void run()
    {
        while(stay<20)
        {
            try {
                stay++;
    //few contions
                Thread.sleep(1000);
            }
       }
  }   

//EveryThing is working fine with thread but when i use SwingUtiities.invokeLater() to call the same class in which this code is there for getting infinite condition. This doesnot redirect it to the class TrafficMainGUI.Is there some other method to achieve this kind of model.

Upvotes: 3

Views: 271

Answers (1)

mKorbel
mKorbel

Reputation: 109813

  1. jTextField1.setText(""+i); must be wrapped in invokeLater for this job by invoked from util.Timer

  2. use Swing Timer instead

  3. if(move.equals("0")) { is about animations, then to use Swing Timer exclusivelly

Upvotes: 3

Related Questions