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 5 of 5

Thread: Assigning an 'int' from a JTextField to a variable...

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Assigning an 'int' from a JTextField to a variable...

    I have a variable checkMate, and a jTextField, I am having problems assigning the value that I input into the jTextField to the variable checkMate. It keeps returning 0, instead of the value I enter.

    private int checkMate;
     
    String userTempLowB = Integer.toString(userTempLow);
     
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        saveUserSettings();
        }
     
     public void saveUserSettings(){
             userTempLowB = jTextField15.getText();
             System.out.println(""+userTempLowB);
         }


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Assigning an 'int' from a JTextField to a variable...

    Hello, can we have more code please? Definition of jTextField15 ? any method calls on it? maybe something compilable? The reason field variable userTempLowB is being assigned when it's defined? are you sure jTextField15 is where you're typing the text?
    Thanks

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Assigning an 'int' from a JTextField to a variable...

    This code snippet doesn't show the attempt to set checkMate to anything. Are you sure you remembered to assign something to checkMate? Also, I'm sure you are aware but just in case, jtextfields getText() method returns a string. Dont forget to parse it.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Assigning an 'int' from a JTextField to a variable...

    Quote Originally Posted by sunde View Post
    This code snippet doesn't show the attempt to set checkMate to anything. Are you sure you remembered to assign something to checkMate? Also, I'm sure you are aware but just in case, jtextfields getText() method returns a string. Dont forget to parse it.
    I think thats the problem I am getting, do I need to say: -

    checkMate = 0;

    Also how do I parse it?, cos' I get a type error when I am trying to assign the value, thats why I used the Integer.toString for userTempLowB, I thought that would convert it, but I think that is wrong.

  5. #5
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Assigning an 'int' from a JTextField to a variable...

    Ok, so do you have the string being properly returned? If so, to convert it, take a look at the different Integer constructors. Then if autoboxing doesn't kick in, take a look for a method that returns an int. Then assign it to checkMate.

    Resources:
    Java 6 Integer (google this, hit feeling lucky button)

Similar Threads

  1. Jtextfield Validation
    By nimishalex in forum AWT / Java Swing
    Replies: 8
    Last Post: December 11th, 2010, 02:42 AM
  2. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 09:13 PM
  3. Get a certain line in a JTextField
    By FlamingDrake in forum Java Theory & Questions
    Replies: 2
    Last Post: May 14th, 2010, 04:21 PM
  4. Assigning variable to an Array
    By sridevi in forum Collections and Generics
    Replies: 3
    Last Post: August 10th, 2009, 11:58 PM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 08:37 AM