Results 1 to 8 of 8
  1. #1
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default break or stop for loop

    Hi everybody..............

    I have small but serious problem. The problem is I have two button -"START" and "STOP"

    So when I will click on START button it simply do the following----

    actionPerformed(ActionEvent e){
    if(e.getSource(STARTbuttonObject){
    for(int i=0;i<=5000;i=i+0.1){
    System.out.println(i);
    }
    }
    }

    now I want to stop the running for loop by clicking on the STOP button. So how can I do it?
    Please explain.
    Thank you.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: break or stop for loop

    You're going to have to take a different approach. Since both of your ActionListeners are going to fire on the same Thread (the EDT), the STOP button has to wait until the START button has completed.

    You might want to look into threading, or adding a Timer.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default Re: break or stop for loop

    Quote Originally Posted by KevinWorkman View Post
    You're going to have to take a different approach. Since both of your ActionListeners are going to fire on the same Thread (the EDT), the STOP button has to wait until the START button has completed.

    You might want to look into threading, or adding a Timer.
    Thanks for you reply.
    Can you please give me a sample code?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: break or stop for loop

    Quote Originally Posted by mitra View Post
    Can you please give me a sample code?
    Hahahaha no. That's not how this works. What have you tried? Where are you stuck?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default Re: break or stop for loop

    Quote Originally Posted by KevinWorkman View Post
    Hahahaha no. That's not how this works. What have you tried? Where are you stuck?
    no actually, thread programming confused me a lot.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: break or stop for loop

    Quote Originally Posted by mitra View Post
    no actually, thread programming confused me a lot.
    What about using a Timer?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default Re: break or stop for loop

    Quote Originally Posted by KevinWorkman View Post
    What about using a Timer?
    I never used Timer till now, but I will check it.

    Thank You very much for giving me ur time.

  8. #8
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: break or stop for loop

    Quote Originally Posted by mitra View Post
    no actually, thread programming confused me a lot.
    probably, but worth learning.

Similar Threads

  1. Replies: 7
    Last Post: 04-02-2011, 06:15 PM
  2. how to stop for loop
    By rjagan in forum New To Java
    Replies: 3
    Last Post: 04-01-2011, 04:15 PM
  3. help!, while loop wont stop!
    By crank6015 in forum New To Java
    Replies: 4
    Last Post: 03-24-2011, 02:27 AM
  4. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 12:50 PM
  5. Replies: 1
    Last Post: 03-03-2009, 12:46 AM

Tags for this Thread

Posting Permissions

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