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 4 of 4

Thread: Loop won't stop. Please help.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loop won't stop. Please help.

    I fixed it. I realized when an action is handled (i.e. clicking on a button) and an ActionListener's methods are called, they are done so in the event handling thread. There's only one thread, and it can only handle one event at a time. So once it starts running your Robot crap, it can't handle any other events until that event's code is finished.

    What you want to do is run your big chunk of code in a separate thread from the event handler. Here's some example code that shows how to do it and how not to do it: http://www.java2s.com/Code/Java/Threads/…

    Essentially what you do is you make a new Thread with all your stuff as the implementation of the run() method. If you need to alter any of your Swing components (to set a label to some text, for example) you would do that using SwingUtilities.invokeLater(). The reason you need to do it with invokeLater() instead of in the Thread's run() is because most of Swing is not thread safe; you could cause serious trouble changing Swing components in other threads. invokeLater() ensures that the component-changing code is run in the event handler thread, where it is safe.
    Last edited by A1one1nDarkness; November 3rd, 2011 at 02:52 AM.


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Loop won't stop. Please help.

    The loop should go through the code 100 times and stop. Remember to put a } at the end incase ur forgetting it.

    Other then that will test ur code when i get home.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Loop won't stop. Please help.

    Quote Originally Posted by A1one1nDarkness View Post
    I fixed it. I realized when an action is handled (i.e. clicking on a button) and an ActionListener's methods are called, they are done so in the event handling thread. There's only one thread, and it can only handle one event at a time. So once it starts running your Robot crap, it can't handle any other events until that event's code is finished.

    What you want to do is run your big chunk of code in a separate thread from the event handler. Here's some example code that shows how to do it and how not to do it: http://www.java2s.com/Code/Java/Threads/…

    Essentially what you do is you make a new Thread with all your stuff as the implementation of the run() method. If you need to alter any of your Swing components (to set a label to some text, for example) you would do that using SwingUtilities.invokeLater(). The reason you need to do it with invokeLater() instead of in the Thread's run() is because most of Swing is not thread safe; you could cause serious trouble changing Swing components in other threads. invokeLater() ensures that the component-changing code is run in the event handler thread, where it is safe.
    And.....???

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Loop won't stop. Please help.

    They dont stick around much after there problem is solved lol

Similar Threads

  1. [SOLVED] Make it stop looping! please. :)
    By Hallowed in forum What's Wrong With My Code?
    Replies: 11
    Last Post: May 7th, 2011, 11:20 AM
  2. I cannot get this loop to stop running. What am I doing wrong??
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 2
    Last Post: May 1st, 2011, 10:28 PM
  3. Button Won't Stop Animation.
    By SyntheticD in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2011, 02:27 PM
  4. Cant get my code to stop jumping
    By Mob31 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 06:03 AM
  5. 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, 07:24 AM