2 Replies Latest reply: Dec 2, 2012 1:01 AM by kapac RSS

    toggle button and scheduled

    kapac
      hi;
      How to make a scheduled task which start and stop with a toggle button. I made the following code which didn't function!!
        but.setOnAction(new EventHandler<ActionEvent>(){      
               @Override
                  public void handle(ActionEvent t) {
                  if(bool)
                       bool=false;
                   else
                       bool=true;
                      if(bool==false){ 
                     myb();
                 
                      }
               
                  }    
            });     
          }
          public void myb () {  
                      final Runnable beeper=new Runnable() {
                  @Override
                  public void run() {
                     clip.play();
                     
                  }          
              };
                   final ScheduledFuture<?> beeperHandle =
                  sched.scheduleAtFixedRate(beeper,500,tempo, TimeUnit.MILLISECONDS);
                   if(bool)
                   beeperHandle.cancel(true);
          } 
        • 1. Re: toggle button and scheduled
          jsmith
          I created a metronome type class which clicks constantly at a given tempo.
          You can find the code for it at: https://gist.github.com/4185890

          The code uses a Timeline rather than creating it's own thread.

          The relevant code is:
          // tick sound from free online audio resource.
          final AudioClip      tick     = new AudioClip("http://www.denhaku.com/r_box/sr16/sr16perc/losticks.wav"); 
          
          // tempo in beats per minute.
          final DoubleProperty tempo    = new SimpleDoubleProperty(100); 
          
          // tick continuously according to the set tempo.
          final Timeline       ticker   = new Timeline();
          ticker.setCycleCount(Animation.INDEFINITE);
          ticker.getKeyFrames().setAll(
            new KeyFrame(Duration.ZERO, new EventHandler<ActionEvent>() {
              @Override public void handle(ActionEvent actionEvent) {
                tick.play();
              }
            }),
            new KeyFrame(Duration.minutes(1), null)
          );
          ticker.rateProperty().bind(tempo);
          
          // setup a toggle buton which can start and stop the ticker.
          final ToggleButton playControl = new ToggleButton("Start");
          playControl.setPrefWidth(60);
          playControl.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override public void changed(ObservableValue<? extends Boolean> observableValue, Boolean wasSelected, Boolean selected) {
              if (selected) {
                ticker.start();
                playControl.setText("Stop");
              } else 
                ticker.stop();
               playControl.setText("Start");
              }
            }
          });
          I did think that you could do this with a PauseTransition on an infinite cycle, but found that that would not work because when you set it to an infinite cycle, the transitions onFinished method only gets executed when the infinite cycle completes (i.e. never) rather than whenever each cycled transition is done. I created http://javafx-jira.kenai.com/browse/RT-26626 to add an onCycleFinished method to Animation, so that you can know when the cycle is done and take action on that. But it's no big deal really, because it is easy enough to do with a timeline.
          • 2. Re: toggle button and scheduled
            kapac
            Thank you for your answer and for your code that i got and made a jar with it .But unfortunately it is irregular on big value 180.i made also a code with timer with the same result which is a major default for a metronome.for days i looked on the web;and i just find one example with very small irregularities:http://briccetti.blogspot.fr/2008/04/simple-java-midi-metronome.html
            it use a thread in a while loop with a reference to system.currenttimemillis() at each turn
            i tried sched.scheduleAtFixedRate(beeper,500,tempo, TimeUnit.MILLISECONDS); which is not bad(but i have the problem to stop an infinite task)