1

I have a java applet that has a class and it has a for loop. What I want is for that loop to stop when I hit the "next" button with the mouse clicked event, in a few words how do I stop or continue the cycle catching the mouseevent of that button that is in a different class than the cycle? , how would that condition be? and can you give an example cause my code it doesn´t finish yet and i dont know how can i keep doing it. Thank you very much.

PM(boolean opcNotes[], int numtries){
Sonido sonido=new Sonido[opcNotes[].length];
for(int i=0;i<sonido.length;i++){
    sonido[i]=sonido.generateSound[opcNotes];}
for(int i<0;i<sonido.length;i++){

    sonido[i].play();
    for(int j=0;j<numtries;j++){

       if(){continue;}     //If i press the "next" button, ill go to the 
                           //next iteration

       else if(){         //If i press the "play again" button, ill do the
                          //action again
          numtries++;
          sonido[i]().play
          }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Not entirely sure what you're trying to do her, but I suggest extending the thread class and making a boolean field that controls said loop. You can use an instance method to change that boolean. – Cardinal System May 09 '18 at 19:20
  • Yes, but i think the real question here is, how can i catch a mouseclickevent o stop the loop?. – Luis Enrique Bermúdez García May 09 '18 at 19:28
  • Check out [this answer](https://stackoverflow.com/questions/16758346/how-pause-and-then-resume-a-thread?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) and see if it helps you. Basically you will have to keep either reference to the running thread or some locking object visible in the button handler and use that to pause the thread. – MatheM May 09 '18 at 20:00
  • You could use a Swing Timer, it can be paused and resumed. The other solution is to maintain the state in a reusable form, then break out of the loop and restart it as required – MadProgrammer May 09 '18 at 20:33
  • Problem solved, i´ve used a thread with notify and wait for mouseclick and i resolved the problem, thank you!. – Luis Enrique Bermúdez García May 13 '18 at 16:34

0 Answers0