Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Ticked Off Timer

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Ticked Off Timer

    I believe this code is written well. It compiled with no errors.

    1. I need help creating a decrementing timer pause of 1 sec.
    2. I also need to make sure the existing pane closes when the new one opens.

    I have been reading and I assume it has something to do with swing.timer but am unsure on how to plug this into the while statement.



    import javax.swing.JOptionPane;
     
    public class Timer{ 
    	public static void main(String[] args){
     
    		String calculateDayString = JOptionPane.showInputDialog("Countdown Timer Enter How Many Days: ");
     
    		int inputDay = Integer.parseInt(calculateDayString);
     
    		int calculateDaySeconds = inputDay * 8640;
     
    		String calculateHourString = JOptionPane.showInputDialog("Countdown Timer Enter How Many Hours: ");
     
    		int inputHour = Integer.parseInt(calculateHourString);
     
    		int calculateHourSeconds = inputHour * 360;
     
    		String calculateMinuteString = JOptionPane.showInputDialog("Countdown Timer Enter How Many Minutes: ");
     
    		int inputMinute = Integer.parseInt(calculateMinuteString);
     
    		int calculateMinuteSeconds = inputMinute * 60;
     
    		String calculateSecondString = JOptionPane.showInputDialog("Countdown Timer Enter How Many Seconds: ");
     
    		int calculateSeconds = Integer.parseInt(calculateSecondString);
     
    		String secondsRemainingString = calculateDaySeconds + calculateHourSeconds + calculateMinuteString + calculateSeconds;
     
    		int secondsRemaining = Integer.parseInt(secondsRemainingString);
     
    		while (secondsRemaining > 0) {
    			secondsRemaining--;
    			System.out.print("You Have " + secondsRemaining);
    		}
     
    		System.out.print("Zero Seconds Timer Stopped");
     
    	} 
     
    }


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Ticked Off Timer

    There's no swing.timer. There is a javax.swing.Timer, most appropriately used in a program that displays a GUI. A program that doesn't create any GUI components may well terminate before the Timer's first cycle is triggered.

    There's also java.util.Timer, which is more general-purpose. Read the API, give it your best shot and come back with a specific question if you can't get it to do what you want it to do.

    db

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ticked Off Timer

    I cant get this to compile but I believe I am now somewhat closer.

    I believe I know what I want to do but I have no Idea or example of how to accomplise the 1000 milisec pause inbetween running the loop down to zero

    import java.swing.Timer;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    import java.awt.event.*;
     
     
    public class Time {
    	public static void main(String[] args) {
     
    	String calculateSecondString = JOptionPane.showInputDialog("Countdown Timer Enter How Many Seconds: ");
     
    	int secondsRemaining = Integer.parseInt(calculateSecondString);
     
    	while (secondsRemaining > 0) {
    		  javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {
              	public void actionPerformed(ActionEvent e) {
    				t.start();
    				System.out.print("You Have " + secondsRemaining);
    				secondsRemaining--;
              	}
           	});
    		System.out.print("Zero Seconds Timer Stopped");
    		t.stop();
    		}
    	}
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Ticked Off Timer

    If all you wish to do is pause execution, use the Thread.sleep function. If you cannot get this to compile, please post the full compiler error message

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ticked Off Timer

    This is my last attempt at this. Can anybody tell me if this actually is working properly because I can get this to compile but I cannot get this to run.


    import java.util.Scanner;
    import javax.swing.JOptionPane;
    import javax.swing.Timer;
     
    import java.awt.event.*;
     
     
    public class Time implements ActionListener{
     
    	private int seconds;
    	private Timer timer;
    	private boolean finished; 
     
    	public Time(int seconds) {
    		this.seconds=seconds;
    		finished=false;
    		timer=new Timer(1000, this);
    	}
     
    	public void start()
    	{
    		timer.start();
    	}
     
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("You Have " + seconds);
    		seconds--;
    		if (seconds==0)
    		{
    			System.out.println("Zero Seconds Timer Stopped");
    			timer.stop();
    			finished=true;
    		}
    		else
    			timer.restart();
    	}
     
    	public boolean isFinished() {
    		return finished;
    	}
     
    	public static void main(String[] args) {
     
    		String calculateSecondString = JOptionPane.showInputDialog("Countdown Timer Enter How Many Seconds: ");
     
    		int seconds = Integer.parseInt(calculateSecondString);
     
    		Time time=new Time(seconds);
    		time.start();
    		while(!time.isFinished());
     
    	}	
    }

  6. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ticked Off Timer

    I need to turn this in today so if somebody can comment on an easier way to do this or make this a little simpler of code than my novice attempt i would appreciate it

  7. #7
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Ticked Off Timer

    I can get this to compile but I cannot get this to run.
    How does it "not run"? Any exception thrown? I'd guess the program does run, but you failed to read and absorb what I said in my first response:
    A program that doesn't create any GUI components may well terminate before the Timer's first cycle is triggered.
    A program that doesn't create any GUI components shouldn't use a javax.swing.Timer.

    db

  8. #8
    Junior Member
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ticked Off Timer

    well I am a very good web traffic monster but my JAVA programming sucks which means when you use terms like GUI the only thing i would be able to tell you is what it stands for because i googled it. This would mean its basically trying to explain to me what a component on a machine in a toothpick factory does because I wouldnt know that either. I only have this problem because I had to retake this course from the Summer which meant that I didnt buy the new edition of this book so none of the timer stuff is in the chapter. I have no idea how to make it run because i cant even read or learn about it in the class im taking.... furthermore I am doing the best I possibly can to absorb hundreds of different "almost" ways of programming using timer I am finding on the internet. I just need this assignment overwith and am williing to pay to have it done. My mac will not run the .class files on any of the programs i have written so im behind another 8 ball at the moment. disaster.

  9. #9
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Ticked Off Timer

    I just need this assignment overwith and am williing to pay to have it done.
    I come here to try to help people who want to learn.

    Goodbye.

    db

  10. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    javapenguin (November 12th, 2010)

Similar Threads

  1. How to Use Timer in Java
    By neo_2010 in forum Java SE API Tutorials
    Replies: 2
    Last Post: August 6th, 2013, 09:49 AM
  2. Timer Class help
    By Deadbob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 23rd, 2010, 12:18 AM
  3. need help with Timer and sound
    By amahara in forum AWT / Java Swing
    Replies: 4
    Last Post: February 18th, 2010, 12:22 PM
  4. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM
  5. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM

Tags for this Thread