2 Replies - 248 Views - Last Post: 05 October 2013 - 02:00 PM Rate Topic: -----

#1 admjhosef  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 42
  • Joined: 02-October 10

TIMER in java Problem cannot setText

Posted 05 October 2013 - 07:32 AM

i am having a problem in my problem

when i set textfield 20 then when i click start button this

the textfield must decrease to 19 and so on

this is my

Code

if(e.getSource() == BCountdownStart)
            {
                String Seconds = TxtCountdownSeconds.getText();
                
                for (int i = Integer.parseInt(Seconds);
			i >= 0; i -= 1)
		{
                        if (i==0) System.exit(0);
			try
			{
				Thread.sleep(1000);
			}
                        
			catch(InterruptedException ie){}
                        revalidate();
                        TxtCountdownSeconds.setText("" + i);
                        
		}
                
            }



Thank You Very Much

Is This A Good Question/Topic? 0
  • +

Replies To: TIMER in java Problem cannot setText

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • Joined: 20-September 08

Re: TIMER in java Problem cannot setText

Posted 05 October 2013 - 10:00 AM

Use javax.swing.Timer to set a text component's text
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: TIMER in java Problem cannot setText

Posted 05 October 2013 - 02:00 PM

This is most probably an actionPerformed() method
it is executed in the actionEvent thread
no painting will be redone until you exit the method so it is completly useless to Thread.sleep() in an actionPerformed() routine

Do as g00se mentionned
Have a Swing.Timer that is called every 1000 ms. In the actionPerformed() of that Timer set your JTextField text
the JTextField will be automatically repaint as soon as you return from the actionPerformed()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1