Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:01:42+00:00 2026-06-01T08:01:42+00:00

I want to limit the what characters the user should be allowed to type

  • 0

I want to limit the what characters the user should be allowed to type in a text field. How can I limit JFormattedTextField to accept only 8 digits of either 0 or 1?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:01:43+00:00Added an answer on June 1, 2026 at 8:01 am

    Use DocumentFilter with Pattern for JTextField, then maybe there isn’t required to use JFormattedTextField

    A sample program to help your cause :

    import java.awt.Toolkit;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.text.AbstractDocument;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DocumentFilter;
    
    public class TextFieldLimit
    {
        private void createAndDisplayGUI()
        {
            JFrame frame = new JFrame("JTextField Limit");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setLocationByPlatform(true);
    
            JPanel contentPane = new JPanel();
    
            JTextField tfield = new JTextField(10);
            /*
             * Here we are using Document Filter
             * to specify the size of the content
             * i.e. 8 in your case.
             */
            ((AbstractDocument)tfield.getDocument()).setDocumentFilter(
                                            new DocumentLimitAndInput(8));
    
            contentPane.add(tfield);
    
            frame.getContentPane().add(contentPane);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TextFieldLimit().createAndDisplayGUI();
                }
            });
        }
    }
    
    class DocumentLimitAndInput extends DocumentFilter
    {
        private int maxCharacters;
    
        public DocumentLimitAndInput(int size)
        {
            maxCharacters = size;
        }
    
        public void insertString(FilterBypass fb, int offset
                                    , String text, AttributeSet aset)
                                            throws BadLocationException
        {
            int size = fb.getDocument().getLength() + text.length();
            /*
             * If the size is less than or equal to the permissible value
             * and the input character is either 0 or 1, only in that
             * case we will allow it to go through, else a BEEP 
             * sound is what a user will hear :-)
             */
            if (size <= maxCharacters && (text.equals("0") || text.equals("1")))
                super.insertString(fb, offset, text, aset);
            else
                Toolkit.getDefaultToolkit().beep();
        }
    
        public void replace(FilterBypass fb, int offset, int length
                                    , String text, AttributeSet aset)
                                            throws BadLocationException
        {
            int size = fb.getDocument().getLength() + text.length();
            /*
             * If the size is less than or equal to the permissible value
             * and the input character is either 0 or 1, only in that
             * case we will allow it to go through, else a BEEP 
             * sound is what a user will hear :-)
             */
            if (size <= maxCharacters && (text.equals("0") || text.equals("1")))
                super.replace(fb, offset, length, text, aset);
            else
                Toolkit.getDefaultToolkit().beep();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to limit the number of characters a user can type in a
I want to limit my varchar columns to have only ascii characters within a
In the YUI datatable I want to limit the number of characters a user
I want to limit the length of a textfield, so that a user can
I'm trying to limit the amount of characters that a user can input. I
I have an input field and I want to limit it to alphanumerical (A-Z,
hey, hi i want put limit on object creation means a class can have
I currently have a string that I want to limit to 200 characters. I
I want to prevent user to enter text in textarea once it reaches a
I have a TextView that I want to limit characters of it. Actually, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.