1

This question is expanding on another I had gotten help with via Stackoverflow ( Update JLabel every X seconds from ArrayList<List> - Java) on making my label update every X seconds. Anyhow... I would now like to Increase or Decrease the speed of the timer and have it loop through the file over an over again.

My print statement looks like so: (int tM is set at 300 currently...)

private void printWords() {
        final Timer timer = new Timer(tM, null);

        ActionListener listener = new ActionListener() {
            private Iterator<Word> w = words.iterator();
            @Override 
            public void actionPerformed(ActionEvent e) {
                if (w.hasNext()) {
                    _textField.setText(w.next().getName());
                    //Prints to Console just Fine...
                    //System.out.println(w.next().getName());   
                }
                else {
                    timer.stop();
                }
            }
        };
        timer.addActionListener(listener);
        bPlay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              timer.start();
            }
          });
        bPause.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              timer.stop();
            }
          });

}

What I would like to do is increase or decrease the speed with a couple of other buttons, Faster and Slower.

How do I go about changing the timer interval mid use?

bFaster.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              tM = 100;
            }
          });
        bSlower.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              tM = 1000;
            }
          });

Thanks for any ideas.

Yours, JF

Community
  • 1
  • 1
jasonflaherty
  • 1,924
  • 7
  • 40
  • 82
  • I found something here: http://stackoverflow.com/questions/6791647/androids-countdowntimer-increase-or-decrease-the-count that notes I cannot do such a thing. Anyone? – jasonflaherty Nov 16 '11 at 19:39

1 Answers1

3

Can't you do the following? The timing won't be perfect but probably not noticeable to the user:

   bFaster.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          tM = 100;
          timer.stop();
          timer.setDelay( tM );
          timer.start();
        }
      });
    bSlower.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          tM = 1000;
          timer.stop();
          timer.setDelay( tM );
          timer.start();
        }
          });
staticman
  • 728
  • 5
  • 9
  • Thanks Staticman! That works just fine for me for sure. Is that an ok way to use the timer in Java? I am pertty new to this... – jasonflaherty Nov 16 '11 at 19:48
  • 3
    +1, but there is no need to stop/restart the Timer, just invoke the setDelay() method. It will take effect the next time the Timer fires. If you stop the Timer, then the initial delay will occur when you start the timer again before the repeat delay kicks in. – camickr Nov 16 '11 at 19:52