-1

I've been trying to implement a timer to my program with no success. What I need is a timer that is able to count in seconds. My idea was to create a timer that added 1 to a variable every second, but I don't really know how to do that. The timer should start when button1 or button2 is clicked, and stop when button3 is clicked. I'll have to use a Swing timer by the way, since it concerns a GUI program.

It would be nice if somebody could point me in the right direction. I have looked up the syntax for timers in Java but I don't understand it (I'm new to coding).

This is the code (I left out some unimportant stuff):

public class ColoredWordsExperiment {
    JFrame frame;
    JButton button1;
    JButton button2;
    JButton button3;
    ButtonHandler buttonHandler;
    Timer timer;

ColoredWordsExperiment(){
    frame = new JFrame("Colored Words Experiment");

    button1 = new JButton("Matching");
    button2 = new JButton("Non-matching");
    button3 = new JButton("Finished");

    buttonHandler = new ButtonHandler(this);
    button1.addActionListener(buttonHandler);
    button2.addActionListener(buttonHandler);
    button3.addActionListener(buttonHandler);  

    timer = new Timer(1000, buttonHandler);
}

public static void main(String[] arg) {
     new ColoredWordsExperiment();
}

}

-

class ButtonHandler implements ActionListener {
    ColoredWordsExperiment coloredWords;
    public ButtonHandler(ColoredWordsExperiment coloredWords) {
    this.coloredWords = coloredWords;
    }

@Override
public void actionPerformed(ActionEvent e){
    //If button "Matching" is clicked  
    if (e.getActionCommand().equals("Matching")) {
        generateMatching();
        timer();

    //If button "Non-Matching is clicked
    } else if (e.getActionCommand().equals("Non-matching")) {
        generateNonMatching();
        timer();

    //If button "Finished" is clicked
    } else if (e.getActionCommand().equals("Finished")) {
        //stop timer
    }

}

public void timer(){
    coloredWords.timer.start();
}

}
Ken
  • 241
  • 7
  • 19
  • 3
    You will want to show us your attempt at solving this first, and you will want to ask a specific question or set of questions telling us exactly where you're stuck and what *specifically* you don't understand. Else your question translates to us, "please do my work for me", and I'm sure that's not what you meant. I'd start by checking out Swing Timers, including Googling for the main tutorial on this, and also searching this site, since this sort of thing is asked commonly. – Hovercraft Full Of Eels Oct 13 '14 at 20:16
  • 1
    [Google search](http://www.google.com/?q=java+swing+timer+tutorial) -- check out the first hit. Also, please check out this [StackOverflow search](https://www.google.com/?q=site:stackoverflow.com+java+swing+stopwatch+timer) – Hovercraft Full Of Eels Oct 13 '14 at 20:19
  • 1
    Why don't you capture the Date when user clicks first button then capture the date when they click the second button then subtract first date from second date to get difference in seconds. No timer needed if that is what you want. – brso05 Oct 13 '14 at 20:27
  • @ Hovercraft Full Of Eels I checked out that link and the stackoverflow searches before but they are quite complicated I think, involving "workers" and "threads" and stuff like that which are still unknown to me. But thanks for the suggestion. – Ken Oct 13 '14 at 20:35
  • 1
    BTW timer doesn't do what you think it does. It allows you to schedule tasks to be done at a future time. It isn't a stopwatch. – Jared Wadsworth Oct 13 '14 at 20:36
  • I think I managed to activate the timer once the button is clicked (see edit in OP). How do I proceed to actually make it add a value of 1 to a certain variable every second? I mean, how can I define the tasks that it should perform once the timer "goes off"? – Ken Oct 13 '14 at 20:48
  • That's where the buttonhandler comes in. Again man, [first hit in my Google search](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) -- it's all there, and it's not really that complicated, not if you read the article. – Hovercraft Full Of Eels Oct 13 '14 at 21:12
  • possible duplicate of [Java code for getting current time](http://stackoverflow.com/questions/833768/java-code-for-getting-current-time) – Rui Marques Oct 14 '14 at 08:47

1 Answers1

1

you could create you're own stopwatch like so:

public class Stopwatch { 

    private long start;

    public Stopwatch() {
        start = 0;
    } 

    public void start() {
        if(start == 0)
            start = System.currentTimeMillis();
    }

    // return time (in seconds) since this object was created
    public double elapsedTime() {
        if(start == 0) { return 0; } //You need to start it first

        long time = (System.currentTimeMillis() - start) / 1000.0;
        start = 0; // reset start to allow you to start it again later
        return time;
    }

Then just create a stopwatch and start it when button 1 or 2 is pushed and retrieve it when button 3 is pushed

Jared Wadsworth
  • 809
  • 5
  • 14