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 tossWinner...in 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:
Eclipse
ASKED:
January 17, 2013 4:41 PM
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.