• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

JAVA GUI Listener,problem..

xrAzVaNx

n00b
Joined
Dec 14, 2004
Messages
51
I was working in java and I am practicing my GUI.
I was doing a little guessing game in which the user inputs a number until he guesses right. Comments such as "Too low" or "Too high..Guess again." are also included between his guesses.
At the end, he is prompted if he wants to play again, a score is calculated and so on..

I have also created a listener so when the user inputs a number in the texfield, and presses the "Guess" button the number is saved into a variable.

The thing is my program will just get to the end without waiting for an input, witthout waiting for the listener. What can I do to tell the program to wait each time for the listener to be hit before it continues its operations??
 
There is nothing to do actually. When you create an GUI application in Java, the Event manager runs on it's own Thread. It will thus, live as long as you don't kill it. Without going in to deep into the problem, I would guess that you called a System.exit() somewhere in your code. As that is the simplest way to kill that Thread.

If that's not it, could you tell me what happens? Does the Frame show up and immediately terminates. Or does it simply not respond when you click the Guess button?
 
Perhaps the program is crashing? Try to see if there are any exceptions being thrown before the program exits. Also, post some code :)

--KK
 
I wasnt clear enough when I said "the program just goes to the end"

It didnt close, it displayed everything, the frame,panel, labels,textfields but thing is,
it also displayed the text that was suposed to be at the end of my program like "Do you want to play another game?" before actually inputting anything..

But thats all i wanted to know, if there was some kind of stop() or pause() method for this.. but i think ill just work on it and try to re-do it maybe in different methods and make it work like it should be..
 
I see. So to answer your question, the event listeners in Java live in another thread. This is by design. What you need to do is, put the code that asks for another game in the buttom listener itself. Such as:

Code:
ButtonListener implements ActionListener //for the guess buttom
{
	public actionPerformed(ActionEvent event)
	{
		// Check to see if the user gave the right answer.

		if (answerIsCorrect)
			System.out.println("Congratulation! Would you like to play again?");
	}
}
 
Back
Top