Results 1 to 19 of 19
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post [SOLVED] How to loop forever

    Hi people, I want to make a procedure in my project to run for as long as the program is running.

    i have tried to use
    Java Code:
    while(true)
    but then, the rest of the program is not executed.

    How can i achieve this??
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  2. #2
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    wow that's the first time I've ever heard of anyone wanting to create an infinite loop... most of us do that by accident. Are you sure that you have thought this through and there isn't a better way to accomplish your goal?

  3. #3
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post

    May be am confused. LOL!
    Anyway, i have to display time on my GUI and what i do is to get the system time then i have to keep the time updated by adding seconds for as long as the program is running. This does not have to interfere with the execution of the program because its just a display of time

    Is there a better way of doing this?
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  4. #4
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    I see, your going to want to create a runnable thread to handle the time so that your whole gui doesn't get locked up. Have you worked with threads before?

  5. #5
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Smile

    Have never used threads before. am just a newbie

    How can i use them in my program?
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  6. #6
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    Lesson: Concurrency (The Java™ Tutorials > Essential Classes) this is a good place to start. It will give you all the info you need, and hopefully give you a good understanding of them.

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    For a periodic GUI action, a Swing Timer would probably be the best choice.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  8. #8
    turanan is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    try

    Java Code:
    for(;;) {
    }

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by turanan View Post
    try

    Java Code:
    for(;;) {
    }
    that's exactly the same as using
    Java Code:
    while(true){ }
    As for the answer to your problem... use a thread or timer. With a timer, I believe you would want to use a java.util.Timer instead of a swing timer for reasons discussed here.

    @OD Swing timers are good, but they DO invoke their actions on the regular event-dispatching thread, so if his GUI can end up needing to complete a large number of tasks, having the timer A: wait for them, or B: create delays for them, well... it would not be pretty.
    Last edited by Singing Boyo; 06-17-2009 at 07:21 PM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  10. #10
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    But the OP wants to display the time and all GUI updates must occur on the event-dispatching thread.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    True... but you dont really want to make the timer wait, so using a java.util.Timer would probably work better... (I'm assuming that both timer's wait for the method they call to be completed to restart. If not, than either approach would work) The below code forces the GUI update to occur on the event-dispatching thread, but the timer action itself does not.

    Java Code:
    TimerTask t = new TimerTask(){
         public void run(){
              EventQueue.invokeLater(new Runnable(){
                   public void run(){
                        clockLabel.updateTime();//updates time on label
                   }
              }
         }
    }
    //create a java.util.Timer with the above TimerTask
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  12. #12
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post

    Am kind of confused now. I dont know where to start from
    This code shows how i want time updated in my GUI

    Java Code:
    jLabel3.setText("<html><Font Color=blue>"
                       +new Date().toString());
     while(true)
     {
            long timeToWait = new GregorianCalendar().getTimeInMillis() + 1000;
            while (new GregorianCalendar().getTimeInMillis() < timeToWait)
                ;
            jLabel3.setText("<html><Font Color=grean>"+new Date().toString());
          }
    How do i bring in the TimerTask??
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

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

    Default

    Quote Originally Posted by Singing Boyo View Post
    True... but you dont really want to make the timer wait, so using a java.util.Timer would probably work better... (I'm assuming that both timer's wait for the method they call to be completed to restart. If not, than either approach would work) The below code forces the GUI update to occur on the event-dispatching thread, but the timer action itself does not.

    Java Code:
    TimerTask t = new TimerTask(){
         public void run(){
              EventQueue.invokeLater(new Runnable(){
                   public void run(){
                        clockLabel.updateTime();//updates time on label
                   }
              }
         }
    }
    //create a java.util.Timer with the above TimerTask
    What do you think a Swing Timer does? It's Thread.sleep is called on a background thread and the actionPerformed is done on the EDT. Using a util Timer in this situation has absolutely no advantages but has the disadvantage of ugly looking code (having to create and queue all the Runnables).

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

    Default

    Quote Originally Posted by Manfizy View Post
    Am kind of confused now. I dont know where to start from
    Start from here: How to Use Swing Timers

  15. #15
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post

    If swing timer has no advantages in this case, why cant you tell me the most applicable to use. Its all about learning the tricks:D
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  16. #16
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You're using Swing already, which has subtle threading requirements, so using a Swing timer is the easiest. Set it up to fire every second, then you can just add a second to the clock and update the GUI in the event method.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  17. #17
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post

    is it supposed to be something like this

    Java Code:
    Timer timer = new Timer(1000,new ActionListener() {
         public void actionPerformed(ActionEvent evt) {
         timer.start();
         jLabel3.setText("<html><Font Color=blue>"
                      +new Date().toString());
    
          long timeToWait = new GregorianCalendar().getTimeInMillis() + 1000;
          while (new GregorianCalendar().getTimeInMillis() < timeToWait)
                
            jLabel3.setText("<html><Font Color=grean>"+new Date().toString());
          }
    
            if (evt.getSource() == jButton2){
            timer.stop();
         
     });
    Correct me where possible
    Last edited by Manfizy; 06-18-2009 at 05:31 PM.
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  18. #18
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    The action performed method should contain only what you want to happen when the timer fires - i.e. add one to the time and update the GUI. There's no need to do any getTimeInMillis(), nor to use a timeToWait variable.

    The timer.start() should go when you want the timer to start, not there. The timer.stop() should go in jButton2's action listener.

    I'll start you off with:
    Java Code:
      int delay = 1000; // fire every second
      ActionListener taskPerformer = new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              // calculate time = time + 1
              String text = String.format("<html><Font Color=blue>%1$tH:%1$tM:%1$tS", time);
              jLabel3.setText(text);
          }
      };
      Timer timer = new Timer(delay, taskPerformer);
      timer.start();
    Last edited by OrangeDog; 06-18-2009 at 07:55 PM.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  19. #19
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Thumbs up

    Thanks alot, i finally got it working

    cheerz!!
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

Similar Threads

  1. Replies: 0
    Last Post: 04-21-2009, 02:32 AM
  2. While loop
    By verbazon in forum New To Java
    Replies: 5
    Last Post: 02-25-2009, 02:29 AM
  3. While loop
    By sjhentges in forum New To Java
    Replies: 11
    Last Post: 11-04-2008, 05:26 PM
  4. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 01:56 AM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 11:20 AM

Posting Permissions

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