Results 1 to 7 of 7
Thread: Need help with timer
- 05-28-2010, 02:29 AM #1
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
Need help with timer
Hi I am new to java Timer and I can't really find a good place to teach it to me. Anyways I tried to take a shot and make a program that lets you input a number to start at then the timer will count down to 0, updating the JLabel all the while. It doesn't work (it compiles, but it won't function like I want it to). I'm sure it has to do with the Timer thing because I really don't know what to do in regards to that.
Java Code:package MeLearningh; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class time extends JFrame { JLabel label, timer; int counter; JTextField tf; JButton button; public time() { setLayout(new GridLayout(2, 2, 5, 5)); label = new JLabel("Enter seconds:", SwingConstants.CENTER); add(label); tf = new JTextField(5); add(tf); button = new JButton("Start Timing"); add(button); timer = new JLabel("Time left: " + counter, SwingConstants.CENTER); add(timer); event e = new event(); button.addActionListener(e); } public class event implements ActionListener { public void actionPerformed(ActionEvent e) { int seconds = (int)(Double.parseDouble(tf.getText())); do { Timer t = new Timer(1000, this); counter--; } while(counter >= seconds); timer.setText("Time left: " + counter); } } public static void main(String args[]) { time gui = new time(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setSize(300,90); gui.setVisible(true); gui.setTitle("Timer Program"); } }
- 05-28-2010, 02:36 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,144
- Rep Power
- 5
Start by reading the section from the Swing tutorial on How to Use Timers. Your code will be much simpler as all you need to do is subtract 1 from your counter every time the Timer fires and the set the text of the label.
- 05-28-2010, 02:43 AM #3
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
I tried reading that but it doesn't really click for me... I added this and now at least it counts.. but not correctly
It starts at like -1 then goes -2, -4, -8, -16 etc etcJava Code:public class event implements ActionListener { public void actionPerformed(ActionEvent e) { int seconds = (int)(Double.parseDouble(tf.getText())); do { Timer t = new Timer(1000, this); t.start(); counter--; } while(counter >= seconds); timer.setText("Time left: " + counter); } }
-
Get that while loop out of there as the Timer itself will do the looping for you. You should not use "this" as an ActionListener as your class will likely need more than one ActionListener -- at least one for JButton(s?), and at least one for your Timer, and they really won't have code that they'll share..
But mostly I second Rob Camick's recommendation: re-read the tutorial and study the sample code intently as it's all there laid out for you. The more you use the tutorials the easier they'll be to use, trust me!
Much luck!
- 05-31-2010, 12:51 AM #5
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
This is the closest I've gotten so far... It starts but it won't time (it just displays the number that was entered into the textbox)
Java Code:package MeLearningh; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class time extends JFrame { JLabel label, timer; int counter; JTextField tf; JButton button; timeClass tc = new timeClass(); Timer timer1 = new Timer(1000, tc); public time() { setLayout(new GridLayout(2, 2, 5, 5)); label = new JLabel("Enter seconds:", SwingConstants.CENTER); add(label); tf = new JTextField(5); add(tf); button = new JButton("Start Timing"); add(button); timer = new JLabel("Time left: " + counter, SwingConstants.CENTER); add(timer); event e = new event(); button.addActionListener(e); } public class event implements ActionListener { public void actionPerformed(ActionEvent e) { timer1.start(); } } public class timeClass implements ActionListener { public void actionPerformed(ActionEvent tc) { int timeStart = (int)(Double.parseDouble(tf.getText())); counter = timeStart; if(counter > 0) { timer.setText("Time left: " + counter); } else { timer1.stop(); } } } public static void main(String args[]) { time gui = new time(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setSize(300,90); gui.setVisible(true); gui.setTitle("Timer Program"); } }
-
Let's look at your timeClass (that should be TimeClass as the first letter of classes should be capitalized):
Java Code:public class TimeClass implements ActionListener { public void actionPerformed(ActionEvent tc) { int timeStart = (int) (Double.parseDouble(tf.getText())); counter = timeStart; if (counter > 0) { timer.setText("Time left: " + counter); } else { timer1.stop(); } } }
The key thing to note here is that everything in the Timer's ActionListener's actionPerformed method will get called each time that the timer loops. And so since your actionPerformed starts with this code:
Java Code:int timeStart = (int) (Double.parseDouble(tf.getText())); counter = timeStart;
Your timer will re-set itself to the number in the JTextField with each tick, and this is not what you want to do. The key here is to set the counter to this number before you start the timer and then decrement the counter in the Timer. For e.g.,
Java Code:@SuppressWarnings("serial") public class FireCatTime extends JFrame { public static final int TIMER_DELAY = 1000; //!! change! JLabel label, timer; int counter; JTextField tf; JButton button; Timer timer1; //!! change public FireCatTime() { setLayout(new GridLayout(2, 2, 5, 5)); label = new JLabel("Enter seconds:", SwingConstants.CENTER); add(label); tf = new JTextField(5); add(tf); button = new JButton("Start Timing"); add(button); timer = new JLabel("Time left: " + counter, SwingConstants.CENTER); add(timer); event e = new event(); button.addActionListener(e); } public class event implements ActionListener { public void actionPerformed(ActionEvent e) { //!! change try { int count = (int)(Double.parseDouble(tf.getText())); timer.setText("Time left: " + count); TimeClass timeClass = new TimeClass(count); timer1 = new Timer(TIMER_DELAY, timeClass); timer1.start(); } catch (NumberFormatException e2) { e2.printStackTrace(); } } } public class TimeClass implements ActionListener { private int counter; //!! when constructing the Timer object, set it's counter public TimeClass(int counter) { this.counter = counter; } public void actionPerformed(ActionEvent tc) { counter--; // decrement the counter if (counter >= 0) { timer.setText("Time left: " + counter); } else { timer1.stop(); } } } public static void main(String args[]) { FireCatTime gui = new FireCatTime(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setSize(300, 90); gui.setVisible(true); gui.setTitle("Timer Program"); } }
- 05-31-2010, 02:38 AM #7
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Timer help
By Kinyo in forum New To JavaReplies: 15Last Post: 03-15-2009, 02:37 AM -
EJB Timer
By mrjunsy in forum Advanced JavaReplies: 0Last Post: 08-22-2008, 04:09 PM -
EJB Timer
By mrjunsy in forum New To JavaReplies: 0Last Post: 08-04-2008, 06:47 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM -
Page Timer
By deeadeed in forum New To JavaReplies: 0Last Post: 12-05-2007, 08:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks