Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Using while-loops in Actionlisteners - WindowBuilder

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default 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:

    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?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Using while-loops in Actionlisteners - WindowBuilder

    Quote Originally Posted by KevinWorkman View Post
    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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Using while-loops in Actionlisteners - WindowBuilder

    Quote Originally Posted by EatMyBible View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    EatMyBible (February 14th, 2013)

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Using while-loops in Actionlisteners - WindowBuilder

    Consider how quickly a computer executes statements. That loop could execute billions of times.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    EatMyBible (February 14th, 2013)

  8. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default 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:

    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?

  9. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Using while-loops in Actionlisteners - WindowBuilder

    Quote Originally Posted by EatMyBible View Post
    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 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. WindowBuilder - beginner problem.
    By EatMyBible in forum AWT / Java Swing
    Replies: 3
    Last Post: February 12th, 2013, 07:30 AM
  2. WindowBuilder Pro
    By Kostas Pap. in forum AWT / Java Swing
    Replies: 1
    Last Post: July 27th, 2012, 09:58 AM
  3. How to use multiple actionlisteners
    By pottsiex5 in forum AWT / Java Swing
    Replies: 9
    Last Post: November 19th, 2011, 09:37 AM
  4. [SOLVED] Quick question regarding ActionListeners
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 2
    Last Post: October 4th, 2011, 12:02 PM
  5. [SOLVED] NullPointerException on ActionListeners
    By cherryduck in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 7th, 2010, 04:17 PM