Controlling text in a JTextField.

I have 2 books on Java and wouldn't you knon it, neither one gives me the information I want!

How can I control what charactors that can be entered and the number of charactors that can be entered into a JTextField?

Thanks in advance.

David, a.k.a. TrainMan
TrainManAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
public class FieldDocument extends PlainDocument
{
     private int limit;

     public FieldDocument(int limit)
     {
          super();
          this.limit = limit;
     }

     public void insertString(int offset, String s, AttributeSet attributeSet)
          throws BadLocationException
     {
          if (limit==0 || getLength() + s.length() < limit)
          {
  // if we haven't reached the limit, insert the string
               super.insertString(offset, s, attributeSet);
          }
          else
          {
                        // otherwise, just lose the string

               Toolkit.getDefaultToolkit().beep();
          }
     
     }
}
0
 
objectsCommented:
use a custom Document.
0
 
objectsCommented:
You can use a similiar technique to limit what characters can be entered.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
TrainManAuthor Commented:
Thanks for the input.  But while this is an answer, it isn't what I'm looking for.  
So let me clearify my question.

How can I do this for each charactor as it is entered?
I do believe that that is posible using MFC C++ in Windows.

David
0
 
objectsCommented:
the above will handle that, insertString() is called whenever any character(s) are inserted into the text field.
0
 
mmuruganandamCommented:
http://www.iam.ubc.ca/guides/javatut99/uiswing/components/textfield.html

This might help you in validating each and every character that you enter.


Otherwise, you can add the key listener to your text fields.  So that you will get the event firing for every key you press.
0
 
mmuruganandamCommented:
http://java.sun.com/developer/JDCTechTips/2001/tt1120.html

This would also help you.  There are more than one way to control your text.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
objects' code should help. Have a look at this for more example:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20910569.html
0
 
TrainManAuthor Commented:
Boy, I asked for help and I sure got it.  Thanks to evaryone.

I'll need a couple of days though to sort through all of the suggestions.

Talk to you then.

TrainMan
0
 
TrainManAuthor Commented:
Sorry everyone.  It  was my intent to split the point to the 3 of you.

TrainMan
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can post a question in Community Support asking them to re-open this question so that you can split between the 3, or you can post points for mmuruganandam and mayankeagle in separate questions, giving links to this one and ask them to post comments on those, so that you can accept them and give the points.
0
All Courses

From novice to tech pro — start learning today.