Results 1 to 3 of 3
  1. #1
    crmbuddy is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Making a program sleep for a second mid-method

    Hey guys so I have a method below which is a button actionListener right:
    Java Code:
    private void attackButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
            if(gamer.isFighting()){
            gamer.attack(monster);
            bLog1.setText("You attacked the " + monster.getName());
            eUpdate();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            monster.defend(gamer);
            bLog1.setText(monster.getName() + " attacked you");
            cUpdate();
            if(monster.getHealth() == 0)
            {
                bLog1.setText(monster.getName() + " defeated!");
            }
            
            if(gamer.getHealth() == 0)
            {
               bLog1.setText("You have died!"); 
            }
            
            }
            
        }
    And where the try/catch statement is, I want the method to stall for a second. The problem is when I click the button and the method is enacted, the method stalls directly at the beginning when I want it to stall at the point its at in the code. Any ideas? Thanks a ton!
    -Crmbuddy

  2. #2
    crmbuddy is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Making a program sleep for a second mid-method

    I've tried timers too and that didnt work so if anyone can help me out thatd be great

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Making a program sleep for a second mid-method

    A small point, but the "sleep" does happen at exactly the point where you invoke that method. Things would get very confusing for programmers if lines of code were reordered by some magic (and undocumented) procedure. But we need to make a distinction between you (the programmer) calling calling setText() with "bLog1.setText("You attacked the " + monster.getName());" and the user actually seeing that text. The Swing runtime will organise for that text to show up but it will happen after your attackButtonActionPerformed() has completely finished.

    And if you sleep() within attackButtonActionPerformed() you are delaying Swing from showing the text.

    Don't call sleep() in event handlers. You've seen one way in which you might frustrate Swing from doing what you intend. But it gets worse. Basically your code has no way of knowing what gui updates might be needed from moment to moment. (Think other application windows being minimised and causing parts of your gui to become visible as just one example). Your event handlers should complete quickly (milliseconds) and certainly not sleep so that the gui does not become sluggish or freeze up.

    If you have actions that you intend to take place later, a timer should be used so that attackButtonActionPerformed() can complete quickly (and the "you attacked..." message appear). Then the timer will call back and allow you to do whatever it is that you intend to happen.

    This raises a little problem: in the 1s between when your program decides that something *should* be done and the time when it actually does it the user may click on things. So (1) you could have a boolean instance variable that you set in attackButtonActionPerformed() to indicate that the action is paused. All buttons and things should check this variable and do nothing if the application has been paused. The variable should be reset when the timer's callback gets invoked. or (2) you could disable the buttons and things that the user might click. This provides a visual indication to the user that the application is paused. Again the controls have to be enabled again in the timer's callback.

    ---

    If you tried timers and they didn't wrok you might like to say why. Most likely it has something to do with your code.

    Speaking of which... If you do post code to illustrate the approach you're taking with timers consider creating a cutdown example which - although it should compile and run - does that but does not contain reams of code that is irrelevant to the actual problem. Creating such a SSCCE means work, but everybody appreciates not having to wade through NB's horrible layout code and method signatures.
    Last edited by pbrockway2; 02-17-2013 at 03:57 AM.

Similar Threads

  1. StringTokenizer, making a method help
    By katiebear128 in forum New To Java
    Replies: 4
    Last Post: 11-15-2011, 12:52 AM
  2. making method, repurposing
    By katiebear128 in forum New To Java
    Replies: 5
    Last Post: 10-27-2011, 04:21 PM
  3. Wait method and sleep method
    By chathura87 in forum New To Java
    Replies: 3
    Last Post: 03-01-2011, 06:04 PM
  4. How to use sleep() method
    By Hasan in forum Threads and Synchronization
    Replies: 12
    Last Post: 10-06-2009, 02:25 PM
  5. How to use sleep method of the Thread class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:42 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
  •