Using while-loops in Actionlisteners - WindowBuilder
Hello. Since the last thread I started in this subforum, I have gained some experience using WindowBuilder. I am preparing for an upcoming test at school (last year of high-school), and as I loathe ActionScript, my teacher allowed me to use Java as long as I am capable of creating a simple GUI. This has worked out great to a certain point, and I have created a game that allows the user to guess a number between 1 and 100, and get a response. (Too high, low or correct.)
However, I also want to add a counter with a variable I have named forsok. (Norwegian for "Tries.") This is supposed to show the number of tries the user have used in order to get the right number. Here is the code for my ActionListener:
Code :
JButton btnGjett = new JButton("Gjett!");
btnGjett.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String tall1 = tall.getText();
int tall2 = Integer.parseInt(tall1);
int forsok = 0;
while (svar != tall2){
forsok++;
}
if (tall2 > svar){
tilbakemelding.setText("Tallet er for høyt!");
}
else if (tall2 < svar){
tilbakemelding.setText("Tallet er for lite!");
}
else{
tilbakemelding.setText("Riktig! Du brukte "+forsok+" forsøk!");
}
}
});
btnGjett.setBounds(10, 36, 91, 23);
contentPane.add(btnGjett);
Please note that the variable svar is defined as a final int outside of the ActionListener, and is not where my problem lies.
My problem lies in the fact that in this case, my program freezes once I push the button. I have come to understand that this is due to my while-loop being inside my ActionListener. However, if I put the while loop outside of the ActionListener, I am forced to convert the forsok-variable to a final int, and final variables are not subject to change.
Any ideas as to how I would go around solving this problem?
Re: Using while-loops in Actionlisteners - WindowBuilder
What exactly is that while loop doing?
I think you might consider refactoring your code. Anonymous inner classes aren't always the way to go.
Re: Using while-loops in Actionlisteners - WindowBuilder
Quote:
Originally Posted by
KevinWorkman
What exactly is that while loop doing?
I think you might consider refactoring your code. Anonymous inner classes aren't always the way to go.
The variable forsok is an integer, which is currently equal to 0. The while loop adds one to forsok as long as tall2 is not equal to svar.
Re: Using while-loops in Actionlisteners - WindowBuilder
Quote:
Originally Posted by
EatMyBible
The variable forsok is an integer, which is currently equal to 0. The while loop adds one to forsok as long as tall2 is not equal to svar.
Right, but why? It sounds like that while loop is never exiting, which is why I suggest reconsidering your logic.
Re: Using while-loops in Actionlisteners - WindowBuilder
Consider how quickly a computer executes statements. That loop could execute billions of times.
Re: Using while-loops in Actionlisteners - WindowBuilder
Once again, you guys helped me and I learned something in the process. That loop would've run countless times, thus it froze the program. Using a loop in that case was meaningless from the beginning, as my intent was to create a counter for how many times the user had guessed a number. Like Kevin said, I reconsidered my logic and realized that once the button is pushed, it goes through all the stuff inside of the ActionListener. Thus, I had to create a private int within the actionlistener, but outside its public void, and simply set forsok++; inside of the listener, as it would run through it every time the button is pressed.
Thanks, appreciate it.
New sourcecode:
Code :
JButton btnGjett = new JButton("Gjett!");
btnGjett.addActionListener(new ActionListener() {
private int forsok;
public void actionPerformed(ActionEvent arg0) {
String tall1 = tall.getText();
int tall2 = Integer.parseInt(tall1);
forsok++;
if (tall2 > svar){
tilbakemelding.setText("Tallet er for høyt!");
}
else if (tall2 < svar){
tilbakemelding.setText("Tallet er for lite!");
}
else{
tilbakemelding.setText("Riktig! Du brukte "+forsok+" forsøk!");
}
}
});
btnGjett.setBounds(10, 36, 91, 23);
contentPane.add(btnGjett);
EDIT: Any suggestions on basic tasks with loops in Swing that I can play around with?
Re: Using while-loops in Actionlisteners - WindowBuilder
Quote:
Originally Posted by
EatMyBible
Once again, you guys helped me and I learned something in the process. That loop would've run countless times, thus it froze the program. Using a loop in that case was meaningless from the beginning, as my intent was to create a counter for how many times the user had guessed a number. Like Kevin said, I reconsidered my logic and realized that once the button is pushed, it goes through all the stuff inside of the ActionListener. Thus, I had to create a private int within the actionlistener, but outside its public void, and simply set forsok++; inside of the listener, as it would run through it every time the button is pressed.
Thanks, appreciate it.
I believe your forsok variable could also be a class variable in the outer class as well, that way you can access it from other methods.
Quote:
Originally Posted by
EatMyBible
EDIT: Any suggestions on basic tasks with loops in Swing that I can play around with?
You could implement a Game of Nim type thing, or a loan/bill calculator, or a grade tracker.. the possibilities are endless, and really depend on your interests.