Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Waiting for a method to call before continuing?

  1. #1
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Waiting for a method to call before continuing?

    Sooo here we go, i made an UI with a MouseListener, mousePressed calls a function.

    Is there any way to make main wait for that function to call before continuing?

    I have 2 things in mind....
    Java Code:
    public static void main(String[] args){
        //Open ui
    
    
    
    }
    public static void program(){
    	//Rest of Program
    		
    }
    or
    Java Code:
    public static void main(String[] args){
        Boolean hasCalled = false;
        while(hasCalled=false){
            sleep(10);
        }
        //rest of program
    }
    public static void startRunning(){
    	hasCalled = true;
    		
    }
    Both of them being ugly, bad and disliked by me... How would you do it?

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

    Default Re: Waiting for a method to call before continuing?

    How would you do it?
    I wouldn't do it in the main() method.

    What task needs to be started by the mouse listener? Have the listener start a new thread to do that task.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Waiting for a method to call before continuing?

    Your while loop is incorrect. I am sure you mean

    Java Code:
    while (hasCalled == false)
    as opposed to

    Java Code:
    while(hasCalled = false) //this will always be false.
    In general, when using boolean in a conditional, don't use == or !=. Just do

    Java Code:
    while (hasCalled)
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Waiting for a method to call before continuing?

    Or this for the not condition:
    Java Code:
    while (!hasCalled)
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Waiting for a method to call before continuing?

    I just wrote that code on the forum....

    More info: its a game(pong), the menu is a JPanel. When it is clicked it should close the panel and open a new panel for the game and start the game loop
    Last edited by Xyexs; 11-08-2013 at 07:36 AM.

  6. #6
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Waiting for a method to call before continuing?

    anyone?

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

    Default Re: Waiting for a method to call before continuing?

    Can you make a small complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Waiting for a method to call before continuing?

    Java Code:
    try {
       Thread.sleep(100); // in milliseconds
    } catch (InterruptedException ie) {
        // empty block
    }
    Note: Normally you should not ignore caught exceptions. However, Thread.sleep() is one instance where most ignore it. In some cases, uses of the
    method require it to be caught.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Waiting for a method to call before continuing?

    Your question implies that you don't understand event-driven programming. Read this: Tutorial: Event-Driven Programming - Browse Files at SourceForge.net
    Norm likes this.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 6
    Last Post: 06-21-2013, 03:42 PM
  2. call a method from another method in same class
    By rockstaedy in forum New To Java
    Replies: 5
    Last Post: 10-03-2012, 02:42 PM
  3. Using Thread waiting() method
    By nicoeschpiko in forum New To Java
    Replies: 7
    Last Post: 12-11-2010, 08:24 PM
  4. Using Thread waiting() method
    By nicoeschpiko in forum Advanced Java
    Replies: 1
    Last Post: 12-11-2010, 03:50 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 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
  •