Results 1 to 8 of 8
  1. #1
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Need help with a gui program

    Hi,im new to java gui programming
    I want to create a loop to display numbers(for example:1 to 20) inside jTextField.
    Here is what I tried:
    Java Code:
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            int i =1;
            int x;
           while (i<=20) {
             x= (Integer.parseInt(jTextField1.getText())+1);
            jTextField1.setText(jTextField1.getText()+ x);
            i++;
           }
        }
    Thank you

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help with a gui program

    What happened when you tried that?

    -----

    Consider using more descriptive variables and methods than jTextField1 and jButton1ActionPerformed(). As the amount of code increases there is a very real payoff from having variables and methods say, by their very text, what they are and do.

  3. #3
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Need help with a gui program

    This is my program:

    When I type 1 into the textfield,I expect it to display numbers from 1 to 20,but what I get is: 1213121412131215
    Last edited by coderX; 02-26-2012 at 09:55 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with a gui program

    If you walk through your code in your head, you'll see that the output you see is what you should expect. Do you know that the + operator has different meanings if you're dealing with Strings vs. numbers, and your method above uses it for both situations, numeric addition and String concatenation.

  5. #5
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Need help with a gui program

    ok,could you please help me to get the expected result?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with a gui program

    Let's see what you're doing, but I'm going to split some of the code up for clarity (and you might wish to do this yourself in future code):

    Java Code:
    while (i<=20) {
      // to replace the line:
      // x= (Integer.parseInt(jTextField1.getText())+1);
      String textFieldText = jTextField1.getText(); // self explanatory
      int x = Integer.parseInt(textFieldText); // self explanatory
      x = x + 1; // A)
    
    
      // to replace the line:
      // jTextField1.setText(jTextField1.getText()+ x);
      String newText = jTextField1.getText();  // B)
      newText = newText + x;  // C)
      jTextField1.setText(newText); //D)
      i++; // self explanatory
    }
    So what's going on?

    1. If the textfield initially holds 1, at this point x will hold 2 -- and this is what you want, I think.
    2. You're getting the String that is held in the JTextField which at this point is "1"
    3. You're doing String concatenation with the textfield's String and x. "1" + 2 = "12"
    4. You're setting the JTextField with this new String, "12".


    So the solution is not to do String concatenation. You should be able to change your code I think to try to improve it. Please give it a go, and see what you can come up with.

    Next we may have to talk about Swing Timers, but let's solve this problem first.

  7. #7
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Need help with a gui program

    I just looked into it today and found out the mistake I made,^Thank you :)
    Here is what I did:
    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
           // TODO add your handling code here:
           int i =0;
           int x=1;
          while (i<=20) {
           jTextField1.setText(jTextField1.getText()+"  " +  x);
           x++;
           i++;
          }
       }

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Need help with a gui program

    Quote Originally Posted by coderX View Post
    Hi,im new to java gui programming
    Quote Originally Posted by coderX View Post
    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    'New to GUI programming' doesn't go well with using a visual designer and code generator.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •