8 Replies - 64773 Views - Last Post: 07 April 2010 - 06:39 PM Rate Topic: -----

#1 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Countdown Timer in a java GUI application

Posted 23 November 2007 - 07:07 PM

Hi. I have done quite a bit of java now but i am pretty new to GUI. What i really need someone to show/help me with is to produce a countdown timer. The programme that i have produced so far is for a car hire service, but when the payments form comes up the customer only has 5 minutes to enter their details otherwise the programme will terminate.
This is my rough form of code
 private class submitJOption implements ActionListener
	{
		int counter=300;
		public void actionPerformed(ActionEvent event)
		{
			Timer clock=new javax.swing.Timer(1000, new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					counter--;
					//update JTextField
				}
			} );
			clock.start();
		}
	}




The problem is that my submitJOption button should bring up the payments form with the countdown timer possibly implimented within the title. As it stands, the user clicks the submit button and a big countdown clock appears. This is where i could do with some help.

Thanks and if you need any more information just gimme a shout.

Is This A Good Question/Topic? 0
  • +

Replies To: Countdown Timer in a java GUI application

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Countdown Timer in a java GUI application

Posted 24 November 2007 - 06:47 AM

I am not sure what do you exactly need help with, but here is a tutorial on timers I hope it helps.
Was This Post Helpful? 1

#3 erietha  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 26-July 09

Re: Countdown Timer in a java GUI application

Posted 20 September 2009 - 09:02 AM

Why don't you just search by goggling first with search keywords "Countdown Timer".. You can found example if you really look for it. Many good example that you can found by goggling..
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,873
  • Joined: 27-December 08

Re: Countdown Timer in a java GUI application

Posted 20 September 2009 - 12:16 PM

What you want to use is a Thread that will count 5 minutes, and stop once the submit button is hit and your form is validated. Here is what I mean:

private class Counter extends Thread{
	public Counter(){}

	public void run(){
		  /*
			1000 milliseconds = 1 second
			60 seconds = 1 minute
			60  * 1000 * 5 = 300,000 milliseconds
			Reduce the thousands place, and you get 300 increments of 1000 milliseconds
		  */
		  for(int i = 300; i >= 0; i -= 1){
				  if(i == 0) System.exit(0);
				  try{
					 Thread.sleep(1000);
					}
				  catch(InterruptedException e){}
				  }
		  }
}



Make this class a private inner class of your GUI, and create an instantiation of it. That way, it can access a JLabel or the title or something. When you instantiate it, do so in your action listener method that watches for when the form should be displayed. Then, start the Thread by using the Object name (non-static method) and call the start() method. If the user enters the data in less than 5 minutes, use the non-static void interrupt() method.
Was This Post Helpful? 1
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Countdown Timer in a java GUI application

Posted 20 September 2009 - 12:18 PM

Wow. You do realise that eritha has revived this post which is over 2 years old. Nevermind, I am sure the code will be useful for somebody. WIll give your well deserved thanks anyways.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,873
  • Joined: 27-December 08

Re: Countdown Timer in a java GUI application

Posted 20 September 2009 - 12:23 PM

Whoops. I didn't look at the dates. I automatically assume all threads w/recent activity are current b/c most threads in this forum die within 12-36 hours.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Countdown Timer in a java GUI application

Posted 20 September 2009 - 12:29 PM

View Postmacosxnerd101, on 20 Sep, 2009 - 12:23 PM, said:

Whoops. I didn't look at the dates. I automatically assume all threads w/recent activity are current b/c most threads in this forum die within 12-36 hours.



I do that as well, its subconscious by now...:lol:
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Frenchpress*


Reputation:

Re: Countdown Timer in a java GUI application

Posted 07 April 2010 - 06:21 PM

View Postmacosxnerd101, on 20 September 2009 - 11:23 AM, said:

Whoops. I didn't look at the dates. I automatically assume all threads w/recent activity are current b/c most threads in this forum die within 12-36 hours.


it was helpful to me, thanks!
Was This Post Helpful? 0

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,873
  • Joined: 27-December 08

Re: Countdown Timer in a java GUI application

Posted 07 April 2010 - 06:39 PM

I'm glad you found that snippet useful. If you're working with Swing GUIs though, you should use Swing Timer as Swing isn't Thread-safe. I didn't learn that until after I posted that snippet. Isn't that the way things always go. :whistling:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1