Results 1 to 17 of 17
  1. #1
    xan.amini is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Unhappy Pausing and starting a loop when a button is pressed.

    Hi,

    In my applet I currently have a loop. I am trying to make a "pause" button. So that when this button is pressed, the loop will pause, and wait for a "start" button to be pressed to continue.

    Any advice?

    Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    What are you doing in the loop? Is there one place in the loop where you want to "pause" the execution?
    And then have it start again at the same place?
    One solution off the top of my head: Break the loop into two parts at the place you want to pause.
    Have a method that calls part one and then part two.
    have a boolean the method can test to see if it is to execute part two. If not, set a flag indicating the loop was paused and exit.
    When the button is pressed, start a Thread to execute the method that calls the two parts. The method can see it was paused and will start the execution at the second part.
    There can be some variables that will have to be saved to be able to restore the state of the loop.

    Another solution could be to put the loop in its own thread and use wait and notify to stop and start the thread.

  3. #3
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    use an event listener and have it when you hit the start button it sets a variable to 0 and when you hit pause it sets the variable to 1. Then in your loop do something like this:
    while(variable < 1){
    //stuff
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that we need to know more about what exactly is going on, that the OP needs to answer Norm's questions before we can give intelligent advice. For all we know all he needs is a Swing Timer that he can pause and restart at will.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Quote Originally Posted by anthropamorphic View Post
    ... when you hit the start button it sets a variable to 0 and when you hit pause it sets the variable to 1.
    Ever heard of a boolean flag?

    db

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    System.out.println(heardOfBoolean);
    Hmmm, how can it be set to false?

  7. #7
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    System.out.println(heardOfBoolean);
    Hmmm, how can it be set to false?
    Simple just type *boolean name* = false;

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    The irony eludes you.

  9. #9
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    The irony eludes you.
    Were you being sarcastic?

  10. #10
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    Ever heard of a boolean flag?

    db
    Yah that would probably be a better way to do it

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    boolean heardOfBoolean = false;
    Do you see the irony in that?

  12. #12
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    boolean heardOfBoolean = false;
    Do you see the irony in that?
    Oh ok now I get it

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    System.out.println(heardOfBoolean);
    Hmmm, how can it be set to false?
    Are you asleep?

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    No. Did you read the rest of the posts to see that I was just having a bit of fun?

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Quote Originally Posted by Junky View Post
    No. Did you read the rest of the posts to see that I was just having a bit of fun?
    You missed the irony of the question that cannot be answered "yes"

  16. #16
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    From my point of analyzation, you need a Javax Timer that increment a variable every 1 sec. then pause it when press the pause button.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by DarrylBurke View Post
    You missed the irony of the question that cannot be answered "yes"
    Doh!

    Actually it can be answered as people have been known to talk in their sleep and have conversations with people who are awake.

Similar Threads

  1. pausing loops?
    By louiedogg418 in forum New To Java
    Replies: 8
    Last Post: 07-05-2011, 11:29 PM
  2. Pausing a Thread
    By Lord Voldemort in forum New To Java
    Replies: 14
    Last Post: 05-22-2011, 04:45 AM
  3. Replies: 3
    Last Post: 01-07-2011, 12:23 AM
  4. Checking if a button was pressed in a Window.
    By Valkyrie in forum New To Java
    Replies: 2
    Last Post: 12-15-2009, 05:28 AM
  5. Waiting for a button to be pressed
    By SomeGuyOverThere in forum New To Java
    Replies: 6
    Last Post: 08-21-2008, 09:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •