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

Thread: Problem: Grabbing Ints from JTextField

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Exclamation Problem: Grabbing Ints from JTextField

    private JTextField JTextField1;
     
    ints number1 = JTextField1;
     
    JTextField1 = New JTextField("",10);
    add(JTextField);

    Anyway to do this, The error is Cannot use JTextField for ints, Something like that, I had this error like a week ago and plan to rewrite it.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem: Grabbing Ints from JTextField

    A text field can contain a String. You need to use a text field method to get that String from the text field.
    When you get its contents into a String you can use the Integer class parsing method to convert that String into an int.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Default Re: Problem: Grabbing Ints from JTextField

    Is there a more simple way to do this?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem: Grabbing Ints from JTextField

    Can you explain what you are trying to do?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Default Re: Problem: Grabbing Ints from JTextField

    Use ints in a jframe? Rather than having to put the information in eclipse itself, i want to be able to put them into a jframe.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem: Grabbing Ints from JTextField

    There is no way for a user to enter int values into a JFrame. A user can put String data into a textfield that is shown in a JFrame.
    Or you could use the Scanner class with a console window and use its nextInt() method to read and convert user input into int values.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading ints into a multidimensional array
    By mju516 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 02:45 PM
  2. Adding doubles and ints.
    By SkyAphid in forum Java Theory & Questions
    Replies: 6
    Last Post: September 8th, 2011, 05:54 PM
  3. JTextField clearing problem
    By rushhour in forum AWT / Java Swing
    Replies: 1
    Last Post: October 24th, 2010, 12:55 PM
  4. How can I avoid JTextArea from grabbing focus upon setting text
    By Orit in forum Java Theory & Questions
    Replies: 4
    Last Post: October 4th, 2010, 05:17 AM
  5. Grabbing info from Java game
    By n0bekre in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2010, 10:32 AM