Posted on 1999-08-02
Medium Priority
Last Modified: 2008-02-01
if I used a JTextField, is there any way that I can limit how many characters can be inputed to the text field?  I want a max of 5 chars to be entered into the text field, and I dont want to have to check the length and pop up a dialog telling the user they've exceeded 5 chars, I just want to flat out limit it.
Question by:mrquija
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Accepted Solution

moirafish earned 240 total points
ID: 1247488
As far as I can see, you have to extend the document model…
This one will even work if you paste in from the clipboard

define your text field:

MaxCharsField jTextField1 = new MaxCharsField(5);

where MaxCharsField & MaxCharsDocument are defined as follows:

import javax.swing.*;
import javax.swing.text.*;

public class MaxCharsField extends JTextField {
  public MaxCharsField(int maxchars) {
      MaxCharsDocument d = (MaxCharsDocument)this.getDocument();
      d.maxchars = maxchars;

  protected Document createDefaultModel() {
    return new MaxCharsDocument();

static class MaxCharsDocument extends PlainDocument {

  int maxchars;

  public void insertString(int offs, String str, AttributeSet a)
    throws BadLocationException {
    int len = this.getLength();
    if (len >= maxchars || str == null) {
    if (len + str.length() > maxchars) {
      str = str.substring(0, maxchars-len);

    super.insertString(offs, str, a);


Author Comment

ID: 1247489
Moirafish, thank you for your wonderful help.  Question, how does your class know how long to construct the textfield?  Is there anyway that I can change the width of the jtextfield to whatever I specify?


Author Comment

ID: 1247490
Nevamind, I used

super.setColumns(int width)

to do what I needed to do. Thanks again

Expert Comment

ID: 1247491
Thanks for the points :-)

The easiest thing to do for the width is just set the column number of the JTextField..

in the constructor
public MaxCharsField(int maxchars)

this means the preferred size of the JTextField will fit maxchars characters
but since the default font of JTextField is variable font, maxchars characters doesn’t always fit the field…
eg: @@@@@ would fill a maxchars 5 field but 11111 would half fill it.

you could set the font to a fixed font such as in MaxCharsField constructor do:

Font monoFont = new Font("Monospaced", Font.PLAIN, 12);

& then the sizing seems to work better if you make maxchars+1 columns, ie.

hope you find something that works!


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question