JTextField

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?
chenweiAsked:
Who is Participating?
 
evijayConnect With a Mentor Commented:
Here is the solution for the problem from java swing book of O Reilly associates

// FixedLengthTextField.java
//
import com.sun.java.swing.*;

// 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);
  }
}

// FixedLengthPlainDocument.java
//
import java.awt.Toolkit;
import com.sun.java.swing.*;
import com.sun.java.swing.text.*;

// 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) {
      Toolkit.getDefaultToolkit().beep();
    }
    else {
      super.insertString(offset, str, a);
    }
  }

  private int maxLength;
}
0
 
conickCommented:
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

0
 
chenweiAuthor Commented:
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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
chenweiAuthor Commented:
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.
0
 
chenweiAuthor Commented:
If I want to use the case of FixedTextField to all the cells of a column in a JTable, how can I do that?
0
 
chenweiAuthor Commented:
Please forget my last comment. I've solved it.
0
 
evijayCommented:
the book name is

java swing
by Orielly associates

vijay

0
All Courses

From novice to tech pro — start learning today.