Feb 07

To restrict the number of characters that the user can enter into a text field can be achieved by either using a JFormattedTextField or using a custom Document with any JTextComponent.

To implement this using a JFormattedTextFiled is simply a matter of specifying the appropriate MaskFormatter to meet your requiremenets.

JTextField field = new JFormattedTextField(new MaskFormatter("***"));

The following class gives an example of how a custom Document can be implemented for use in any text component.

public class FixedSizeDocument extends PlainDocument
{
   private int max = 10;
   
   public FixedSizeDocument(int max) 
   { 
        this.max = max; 
   } 

   @Override
   public void insertString(int offs, String str, AttributeSet a)
      throws BadLocationException
   {
      // check string being inserted does not exceed max length
	   
      if (getLength()+str.length()>max)
      {
         // If it does, then truncate it
    	  
         str = str.substring(0, max - getLength());
      }
      super.insertString(offs, str, a);
   }
}

You can then use this class with your JTextField for example using the following

field.setDocument(new FixedSizeDocument(5));

written by objects \\ tags: , , , , ,


19 Responses to “How to limit number of characters in JTextField?”

  1. Mr Swing Says:

    Hi. This was just great. Much better and easier than suggestion at Sun and other Java sites.

  2. objects Says:

    Glad you liked it, thanks for the feedback

  3. Blue Says:

    Mr Swing is right. I tried this solution and it worked great!!! This is the best solution that I found on the web and I’ve been searching for days already and I always ended up being linked to those complicated solutions on different websites. Thanks a lot!!! I really really appreciate it

  4. Ken Says:

    Best solution on the web. Far simpler than the many other “solutions” I found. Thanks.

  5. objects Says:

    Thanks, glad you found it useful.

  6. Eric Cute Says:

    Wow. This solution really worked and very simple to understand for java beginners like me. Thank a lot!

  7. objects Says:

    Thanks Eric.

  8. Anu Says:

    Great job

  9. dan Says:

    really easy, I like it

  10. Una Says:

    Set up a keytyped event on the textfield

    if (jTextField1.getText().length()>=2){

    evt.consume();

    }

  11. objects Says:

    Using a KeyListener will be problematic for cut and paste, and if the text field change is not caused by typing (eg. if its value is programatically changed)

  12. vanb Says:

    Thanks….
    This solution helped me and it works fine.

  13. prakash Says:

    thanks for the post, helped me..

  14. Marco Says:

    Thanks a lot, really usefull! :-D

  15. Ahamed Rafeeque Says:

    Thank you very much and hopes post like this in future as well.

  16. nic Says:

    having errors, cannot find symbol = AttributeSet

  17. objects Says:

    import javax.swing.text.AttributeSet;

  18. Broomes Says:

    Thanks for this code. It didn’t work well for me as I am a beginner to Java and didn’t know where to place “field.setDocument(new FixedSizeDocument(5));”. So, I made some an adjustment. I placed it in the “customize code” section of the JTextField, which was alreay renamed for my programming efforts. Let’s say I called it crazyTextField. It was already renewed as crazyTextField = new javax.swing.JTextField();. So, I tossed the “JTextField field = new JFormattedTextField(new MaskFormatter(“***”));” and changed your other line of code to “crazyTextField.setDocument(new FixedSizeDocument(1));”. Worked like a charm! Thanks again.

  19. Yelili Says:

    Really great !! It’s working perfectly ! A big Thank you for Objects ;)

Leave a Reply

You must be logged in to post a comment.