Results 1 to 6 of 6

Thread: TextFields

  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Question TextFields

    how can I set a textfield for only integers.I want double datatype everytime to put in textfield.I cannot do it with setText() .. any help ???

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: TextFields

    You need to convert your data to a String first. All Wrapper classes have a toString method.

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: TextFields

    how can I do it ? Syntax please

  4. #4
    b4viral is offline Member
    Join Date
    Jul 2012
    Posts
    17
    Rep Power
    0

    Default Re: TextFields

    int i=0;
    String str = i+"";
    System.out.println(str);

    Try running this...
    for syntax, the great "Google" is there my friend :)

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: TextFields

    Quote Originally Posted by allaudin View Post
    how can I do it ? Syntax please
    No syntax but semantics: read the API documentation for the DocumentFilter class.

    kind regards,

    Jos
    Last edited by JosAH; 05-14-2013 at 04:24 PM. Reason: it's a class, not an interface.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: TextFields

    Your Question is not clear....
    I Assume you want to restrict users from entering things like "Kagiso" which cannot be converted to ints
    try something like this

    boolean done = true;
    while (! done)
    try {
    int myInt = Interger.parseInt(myTextField.getText()); // getText will return a string that was typed into the textfield
    done = true; // to break out of the loop
    }
    catch(NumberFormatException e)
    {
    // print some message like "Invalid entry" ...... most people would use a dialog box
    myTextField.setText(""); //to clear the textField
    done = false;
    }


    I hope this becomes helpful ...
    Post your code along with your question
    Last edited by Kagiso; 05-15-2013 at 07:08 PM.

Similar Threads

  1. Help with empty textfields!!!
    By Aash96 in forum New To Java
    Replies: 3
    Last Post: 12-13-2012, 08:49 PM
  2. textfields in forms
    By lolzord in forum New To Java
    Replies: 5
    Last Post: 10-22-2012, 08:00 PM
  3. TextFields not showing up
    By Army in forum New To Java
    Replies: 7
    Last Post: 06-20-2012, 07:36 PM
  4. textfields - cursor
    By newbie123 in forum AWT / Swing
    Replies: 6
    Last Post: 09-29-2011, 07:39 PM
  5. information from a textfields
    By bbq in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-28-2007, 06:28 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
  •