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

Thread: How to put a default int, float, ect. for a JTextField ?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to put a default int, float, ect. for a JTextField ?

    Hello every one.

    Will I'm tying in my code to set a default number for the JTextField that when the user decide not to put any numbers. Like let say that I want the textfield set to 0 , so then the user do not file it it won't make any problem to the program because its already has a default number.

    ****************
     
    if (stringGQ != null && stringGW != null && stringGP != null){
     
    stringGQ = gMQ.getText();
    stringGW = gMW.getText();
    stringGP = gMP.getText();
     
    weightPrice_1M = Double.parseDouble(stringGW) * Double.parseDouble(stringGP);		 
    total_GroundM += weightPrice_1M  * Integer.parseInt(stringGQ);
     
    finalTotal += total_GroundM;
     
      }
     
    else{
     
     gMQ.getText().valueOf(0);
     gMW.getText().valueOf(0);
    gMP.getText().valueOf(0);						   				  					  
     
     
    }

    Note: This is a small part of my code. when I leave it empty it take the 0 as a value, However,
    when I write in text field it also take the value of a 0 and the finalTotal is also = to 0

    So could please someone what I'm doing wrong.

    Thank you in front


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: How to put a default int, float, ect. for a JTextField ?

    Quote Originally Posted by Dragon3002 View Post
    Will I'm tying in my code to set a default number for the JTextField that when the user decide not to put any numbers.
    First, get the string from JTextField (getText()). Second, test if string is empty (equals to "" or length is 0). If it's empty, you can assign to your int/long/etc. variable a "default" value (0, or what you decide). If string is not empty, attempt to "parse" the string using Integer.parseInt, Long.parseLong, etc. If NumberFormatException is thrown you can: a) inform the user (possible input retry), or b) ignore error and assign again a "default" value.
    That's all.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

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

    Default Re: How to put a default int, float, ect. for a JTextField ?

    set a default number for the JTextField
    See the constructor for the JTextField class. It will take a default value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Bulk put(int) for ByteBuffer
    By Cornix in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2013, 03:55 AM
  2. How to create an Int or double of what the user types int a JTextField?
    By Speedstack79 in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2013, 11:00 PM
  3. [SOLVED] how to put changeable value in JTextField?
    By scorpas in forum Java Applets
    Replies: 34
    Last Post: May 21st, 2012, 05:24 PM
  4. Assign Default values to the JTextfield in java swing
    By sjohnfernandas in forum AWT / Java Swing
    Replies: 1
    Last Post: December 17th, 2011, 06:59 AM
  5. can we select default writing language to JTextField
    By bravvve in forum AWT / Java Swing
    Replies: 0
    Last Post: March 10th, 2011, 03:51 AM

Tags for this Thread