Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Pausing a loop until a button is pressed

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pausing a loop until a button is pressed

    For my code the most convenient method is to use a for loop. This works well, but at the end of each iteration I want the user to choose between on button, one that continues the loop and another that ends it. I can do this using break/changing variable, but I don't see how I can make my loop wait for the user to press the button after each time. Is there some code that I should put at the end of my loop?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Pausing a loop until a button is pressed

    Turn your for loop code into a class and use it as an ActionListener for the button. Every time the button gets pressed run one iteration. Variables which you want to keep persistent between iterations will need to be class fields.

Similar Threads

  1. Trying to start a game when 'Enter' is pressed.
    By Shaybay92 in forum AWT / Java Swing
    Replies: 6
    Last Post: September 27th, 2011, 07:48 AM
  2. Is Key Pressed statements
    By Yo Cas Cas in forum AWT / Java Swing
    Replies: 6
    Last Post: August 27th, 2011, 12:48 AM
  3. Stop a while loop with an awt button, problem
    By frx08 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 20th, 2011, 08:24 AM
  4. Move button with mouse pressed
    By Stavros in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 10th, 2010, 08:45 AM
  5. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM