Results 1 to 4 of 4
  1. #1
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default Allow only numerical input on JTextField

    I am using netbeans to create a GUI with swing/matisse. I am trying to create a Phone Number and zip code FormattedJtextFields. How do I make it so only numbers can be entered? I have been reading tutorials about mask formatter but I am creating the JTextFields with GUI tools, not from scratch so how can I use these features?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by patriotsfan View Post
    I am using netbeans to create a GUI with swing/matisse. I am trying to create a Phone Number and zip code FormattedJtextFields. How do I make it so only numbers can be entered? I have been reading tutorials about mask formatter but I am creating the JTextFields with GUI tools, not from scratch so how can I use these features?
    If using NetBeans, it can create the Formatter for your JFormattedTextField for you. Simply create the JFormattedTextField, then scroll through its properties til you get to formatterFactory, and click on the button with the ellipsis "..." on it. Then follow the instructions, and you're golden.

  3. #3
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default

    you can use the restrictions in Jtextfield follow the code to enter only numbers in the JtextField..

    class LimitedText extends InputVerify
    {
    private int limit;
    private boolean toUppercase = false;
    public LimitedText(int limit)
    {
    super();
    this.limit = limit;
    }
    public LimitedText(int limit,boolean upper)
    {
    super();
    this.limit = limit;
    toUppercase = upper;
    }
    @Override
    public void insertString(int offs,String str,AttributeSet a) throws BadLocationException
    {
    if (limit == 0 || getLength() + str.length() <= limit)
    {
    if ((getLength() + str.length()) <= limit)
    {
    if (toUppercase) str = str.toUpperCase();
    super.insertString(offs, str, a);
    }
    }
    }
    }

    class InputVerify extends PlainDocument
    {
    @Override
    public void insertString(int offs,String str,AttributeSet a)throws BadLocationException
    {
    if(str==null)
    return;
    char [] before = str.toCharArray();
    String after = "";
    for(int i=0;i < before.length;i++)
    {
    if( ((before[i]>='a' && before[i]<='f') || (before[i]>='A' && before[i]<='F')) || ((before[i]>='0' && before[i]<='9') || (before[i]>='0' && before[i]<='9')) )
    after += before[i];
    }
    super.insertString(offs,after,a);
    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rjagan View Post
    you can use the restrictions in Jtextfield follow the code to enter only numbers in the JtextField..

    Java Code:
    class LimitedText extends InputVerify
        {
            private int limit;
            private boolean toUppercase = false;
            public LimitedText(int limit)
            {
                super();
                this.limit = limit;
            }
            public LimitedText(int limit,boolean upper)
            {
                super();
                this.limit = limit;
                toUppercase = upper;
            }
        @Override
        public void insertString(int offs,String str,AttributeSet a) throws BadLocationException
        {
            if (limit == 0 || getLength() + str.length() <= limit)
            {
                if ((getLength() + str.length()) <= limit)
                {
                    if (toUppercase) str = str.toUpperCase();
                    super.insertString(offs, str, a);
                }
            }
        }
        }
    
        class InputVerify extends PlainDocument
        {
            @Override
            public void insertString(int offs,String str,AttributeSet a)throws BadLocationException
            {
                if(str==null)
                return;
                char [] before = str.toCharArray();
                String after = "";
                for(int i=0;i < before.length;i++)
                {
                    if( ((before[i]>='a' && before[i]<='f') || (before[i]>='A' && before[i]<='F')) || ((before[i]>='0' && before[i]<='9') || (before[i]>='0' && before[i]<='9'))  )
                        after += before[i];
                }
                super.insertString(offs,after,a);
            }
        }
    If you're going to be doing this with a plain JTextField, the way to limit in put is with a DocumentFilter. No overriding of a component is necessary.

Similar Threads

  1. Dynamically input text JTextField. Is it possible?
    By africanhacker in forum New To Java
    Replies: 2
    Last Post: 03-21-2011, 04:23 PM
  2. JTextfield renderer/editor input map
    By mine0926 in forum New To Java
    Replies: 1
    Last Post: 10-19-2010, 06:26 AM
  3. Replies: 7
    Last Post: 04-10-2010, 11:31 AM
  4. Convert Input from JTextField to int?
    By jls7168 in forum New To Java
    Replies: 7
    Last Post: 02-20-2009, 02:29 AM
  5. Constraining Input in JTextField
    By kataya in forum AWT / Swing
    Replies: 1
    Last Post: 06-26-2008, 06:25 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
  •