Results 1 to 5 of 5
  1. #1
    sandysm is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Post how to get integer value from textfield

    public void actionPerformed(ActionEvent ae){

    //getText() returns string
    String i=txtf.getText();

    /*below statement gives NumberFormatException, i want to retrieve integer from textfield */

    int s=Integer.parseInt(i);
    //code for calculation on integer

    /*displaying integer value agn to textfield or any other label */

    txtf.setText(i);
    }

    which methods/eventlistener should i use, to get text in integer format from textfield & display it on label/textfield.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sandysm View Post
    public void actionPerformed(ActionEvent ae){

    //getText() returns string
    String i=txtf.getText();

    /*below statement gives NumberFormatException, i want to retrieve integer from textfield */

    int s=Integer.parseInt(i);
    //code for calculation on integer

    /*displaying integer value agn to textfield or any other label */

    txtf.setText(i);
    }

    which methods/eventlistener should i use, to get text in integer format from textfield & display it on label/textfield.
    Your program logic is fine but the String you got from your text field doesn't make a valid integer; did you try a simple System.out.println(i) just before you are trying to convert it to an int and see what was in the text field?

    kind regards,

    Jos

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    There is an API that ensures text field format compliance, which is a JFormattedTextField.

    To use it to automatically enforce an integer,

    Java Code:
    import java.text.*;
    ...the other usual swing and awt imports
    
    Format format = NumberFormat.getIntegerInstance();
    JFormattedTextField jtf = new JFormattedTextField(format);
    
    jtf.addActionListener(...
    ...at least I think that's the right listener to only notify you when the user has entered a valid integer. You can make it a little nicer by also adding a keylistener and using the getText method of the textField combined with the format method of the Format (in a try catch) to change the text color to red while the user is typing, if what they are typing is not valid.

  4. #4
    g_ayyappan is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Perhaps, you could register a key event. Whenever a non-digit is entered in text box, you could block the user from entering it.

    Regards
    Ayyappan Gandhirajan

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

    Default

    Quote Originally Posted by g_ayyappan View Post
    Perhaps, you could register a key event. Whenever a non-digit is entered in text box, you could block the user from entering it.
    The answer already has been given in reply #3.

    kind regards,

    Jos

Similar Threads

  1. TextField Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:21 PM
  2. textfield and database
    By t3mh4 in forum JDBC
    Replies: 2
    Last Post: 05-14-2008, 09:12 AM
  3. JSP - getting value from a textfield
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:58 PM
  4. alignment of textfield in awt
    By nitinborge5 in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 12:16 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
  •