I made a custom JTextField that only accepts numbers into the textfield. I used insertString to do this. Tt works pretty well except it allows oddly only the letter e. It accept e only after a number is inputted first. And once e has been entered the textfield does not allow any more input unless the letter is erased. All I need to know is what I'm doing wrong. Here is the code.

//Imports
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

public class JNumericField  extends JTextField{

    //Constructors 
    //sets the constructor of JTextField to nothing
    public JNumericField()
    {
        super();
    }

    //sets the constructor of JTextField to  make the length text field 
    public JNumericField(int size)
    {
        super(size);
    }

    //sets the constructor of JTextField to display text in the text field
    public JNumericField(String string)
    {
        super(string);
    }

    //sets the constructor of JTextField to display text in the text field and set the length of the text field
    public JNumericField(String string, int size)
    {
        super(string, size);
    }

    //sets the contructor of the JTextField to display text, set the size, and sends a document object
    public JNumericField(Document doc, String string, int size)
    {
        super(doc, string, size);
    }

    //This Method gets the number from the text field and converts it to a Double
    public double getNumber()
    {
            String x = getText(); //Gets the text from the text field
            double d = 0.0; //will hold the number after it has been parsed to a double
            //Catches and handles NumberFormatException
            try
            {
                d = Double.parseDouble(x); //parses the number to a double
                return d; // Returns the parsed input
            }
            catch(NumberFormatException e)
            {
               //If NumberFormatException is thrown display an error message
               JOptionPane.showMessageDialog( this, "Not Numeric Input", "", JOptionPane.ERROR_MESSAGE );
            }
            return d; //Returns d       
    }

    //This method uses DoubleDocument to handle the textfield 
    protected Document createDefaultModel() {
        return new DoubleDocument();
    }

    //This class makes sure that only number are displayed in the text field
    static class DoubleDocument extends PlainDocument 
    {
        //insertString Method uses the information from the text field to know what is in it and whethor not to except it
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
        {
            //if str does is null do nothing
            if(str == null) 
                return;
            //The string will hold the text that is in the text field
            String string = getText(0,getLength()) + str + getText(0, getLength());

            //if there is a negative sign at the beginning of the text field let it in
            if(offs == 0 && str.equals("-"))
            {
               super.insertString(offs, str, a); 
            }
            else
            {
                //catches and handles NumberFormatException
                try
                {
                    Double.parseDouble(string); //Changes string to a Double

                    super.insertString(offs, str, a); //displays it on the text field

                }
                catch(NumberFormatException e)
                {
                    //if NumberFormatExcpetion what thrown do nothing
                }
            }
        }
    }

}

Edited 4 Years Ago by devninja: Had to add a space

don't hide exceptions (that is what you do here:

            try
            {
                Double.parseDouble(string); //Changes string to a Double
                super.insertString(offs, str, a); //displays it on the text field
            }
            catch(NumberFormatException e)
            {
                //if NumberFormatExcpetion what thrown do nothing
            }

just add a e.printStackTrace(); inside that catch block tho show what happens

This article has been dead for over six months. Start a new discussion instead.