Posted on 1998-11-27
Last Modified: 2009-12-16
I want to put a JTextField in my program and the textfield can accept n characters. If the entered characters exceed n, a beep will be generated and the >n character can't be entered anymore.

I am succeded to generate the beep if the entered character exceeds n, but I failed to stop the further entered character. That means, thought a beep can be generated if the length of entered chracters longer than n, but one can further enter characters and that's what I want to solve.

Has anyone idea?
Question by:chenwei
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
  • 4
  • 2

Expert Comment

ID: 1228333
Assuming you have a textListener added to the textfield to generate the beep when >n
in the same method couldnt you get the text (getText()) when =n and save it in a String so that when another key is entered you can call setText(String) so it gives the appearance that no more characters are being entered.
Maybe Im misunderstanding the question though... If you could post the code that you are currently using maybe I (or someone else) could come up with something better


Author Comment

ID: 1228334
To conick:

thanks for your message. But truly to say I have the same idea as you at the begining. But I don't think this is a good solution. I've used the Exception class to handle it. It's much intellent.

Sorry, I can't accept your suggestion. I've solved the problem myself.

Accepted Solution

evijay earned 20 total points
ID: 1228335
Here is the solution for the problem from java swing book of O Reilly associates


// A fixed-length JTextField
public class FixedLengthTextField extends JTextField {

  public FixedLengthTextField(int length) {
    this(null, length);

  public FixedLengthTextField(String text, int length) {
    super(new FixedLengthPlainDocument(length), text, length);

import java.awt.Toolkit;

// An extension of PlainDocument that restricts the length of the content it
// contains.
public class FixedLengthPlainDocument extends PlainDocument {

  // Create a new document with the given max length
  public FixedLengthPlainDocument(int maxLength) {
    this.maxLength = maxLength;

  // If this insertion would exceed the maximum document length, we "beep" and do
  // nothing else. Otherwise, super.insertString() is called.
  public void insertString(int offset, String str, AttributeSet a)
  throws BadLocationException {
    if (getLength() + str.length() > maxLength) {
    else {
      super.insertString(offset, str, a);

  private int maxLength;
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


Author Comment

ID: 1228336
To, Evijay:

thanks for the answer. truly to say I've solved the problem my self. I used almost the same way you told me. Sure, I will give you the points. But I will  like to ask you if you could give the book's title you mentioned. I guess there are also some other useful examples in this book. If you could also recommend me some othere interesting books, it would be the best.

Author Comment

ID: 1228337
If I want to use the case of FixedTextField to all the cells of a column in a JTable, how can I do that?

Author Comment

ID: 1228338
Please forget my last comment. I've solved it.

Expert Comment

ID: 1228339
the book name is

java swing
by Orielly associates



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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

705 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