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

  • SEARCH
  • Home
  • 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 6597731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:14:45+00:00 2026-05-25T18:14:45+00:00

In text field more than 10 characters na, it has to show an error.

  • 0

In text field more than 10 characters na, it has to show an error. For that i used document filter:

JTextField field = (JTextField) txtFld;
AbstractDocument document = (AbstractDocument) field.getDocument();
document.setDocumentFilter(new DocumentSizeAndUppercaseFilter(10));

So this is my document filter coding. I registered the textfield through document filter. But nothing has happening here. How to use document filter?

DocumentSizeAndUppercaseFilter class which has the error msg.

  • 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-05-25T18:14:46+00:00Added an answer on May 25, 2026 at 6:14 pm

    Without seeing the code for DocumentSizeAndUppercaseFilter I would suspect that you havn’t implemented (/override) the DocumentFilter‘s replace method:

    @Override
    public void replace(DocumentFilter.FilterBypass fb, int offset,
                        int length, String text, AttributeSet attrs)
            throws BadLocationException {
    
        ....
    }
    

    Screenshot from the code below:

    screenshot


    Example implementation of DocumentSizeAndUppercaseFilter:

    static class DocumentSizeAndUppercaseFilter extends DocumentFilter {
    
        private final int max;
    
        public DocumentSizeAndUppercaseFilter(int max) {
            this.max = max;
        }
    
        @Override
        public void insertString(DocumentFilter.FilterBypass fb, int offset,
                                 String text, AttributeSet attr) 
                throws BadLocationException {
            if (fb.getDocument().getLength() + text.length() < max)
                super.insertString(fb, offset, text.toUpperCase(), attr);
            else 
                showError();
        }
    
        @Override
        public void replace(DocumentFilter.FilterBypass fb, int offset,
                            int length, String text, AttributeSet attrs)
                throws BadLocationException {
            int documentLength = fb.getDocument().getLength();
            if (documentLength - length + text.length() < max)
                super.replace(fb, offset, length, text.toUpperCase(), attrs);
            else 
                showError();
        }
    
        private void showError() {
            JOptionPane.showMessageDialog(null, "Too many characters entered");
        }
    }
    

    Example main:

    public static void main(String[] args) {
    
        JTextField firstName = new JTextField();
        AbstractDocument d = (AbstractDocument) firstName.getDocument();
        d.setDocumentFilter(new DocumentSizeAndUppercaseFilter(10));
    
        JFrame frame = new JFrame("Test");
        frame.add(firstName);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 60);
        frame.setVisible(true);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A Text field ( SPFieldText ) has a limit of 255 characters. What, if
How to find the text field data has \n in mysql? for ex :
I have an input text field, which has a value something by default, but
I have an input text field which has style: visibility: visible or style: visibility:
I have a select field where my option value is more than around 250
I am currently restricting the user to write in a text from more than
How do I create more than 40-50 textfields and labels in a single view
If I have a text field with SWT, how can I get the field
I have a text field (flash.display.textField) for editing, but the default font does not
I'm validating a text field in my form as follows: $name = new Zend_Form_Element_Text('name');

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.