how to stop execution of a code until a Jwindow is closed?

Hi this might be tuff to understand- what i m trying to do is i m creating an object of a JWindow class into another class... like this:- public class GamePlay extends JPanel{ TossWindow toss; public GamePlay(){ Toss(); } public void Toss(){ toss=new TossWindow(team1Name,team2Name); System.out.println(toss.tossWinner); } } in above code TossWindow is a class that extends JWindow and in that class i have a variable named that JWindow i have some mechanism for the user to select either 'Heads' or 'Tails' and perform some calculation, and finally it gets decided that which team has won the toss and name of the winning team gets inserted into tossWinner. Now as i have shown in the above code in Toss() method first the instance of 'toss' gets initiated and i am able to do whatever i want to do in that TossWindow,it gets visible at that time...but at the same time the second line i.e. "System.out.println()" also gets executed and it does not wait for the TossWindow to complete its execution so that i may know the winner of the Toss...instead null gets printed on the console... I want to stop the code until TossWindow has Completed its execution... Can u Sort out a way for me to do this...???

Software/Hardware used:

Answer Wiki

Thanks. We'll let you know when a new response is added.

Firstly we can help you more if you post some code to examine. From what i gather, your problem has to do with your loops or threads in the program. You need to retrace the code to see what gets executed when.

You should create a method and give it a name like getToss() which will return the toss variable. getToss() will be called just before the System.out.println() statement. Hope this helps.

Discuss This Question: 1  Reply

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • carlosdl
    Some clarification is needed here. If this doesn't make sense, it will show that I didn't understand your explanation: 1) The only possible way that "System.out.println()" could be executed when the previous method call has not ended yet would be if such method (the TossWindow constructor) were running in a separate thread, but I don't think this is the case. 2) You say that the second line is executed "at the same time", which is also not possible. This is what is happening: The TossWindow constructor is being executed, and when it finishes the println call is executed, as expected, but you have to keep in mind that any interaction from the user is not part of the execution of the TossWindow constructor, and also, any event triggered by this user interaction will be handled by separate methods, outside of the TossWindow constructor. Sounds to me that you could put your println call (and any other code you want to execute at that point) in a separate method, which could be called from the method that handles the user selection and makes the necessary calculations to determine the winner.
    85,885 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: