Solved

Controlling text in a JTextField.

Posted on 2004-03-30
11
216 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:TrainMan
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10719792
use a custom Document.
0
 
LVL 92

Accepted Solution

by:
objects earned 60 total points
ID: 10719800
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
 
LVL 92

Expert Comment

by:objects
ID: 10719809
You can use a similiar technique to limit what characters can be entered.
0
 

Author Comment

by:TrainMan
ID: 10720235
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
 
LVL 92

Expert Comment

by:objects
ID: 10720270
the above will handle that, insertString() is called whenever any character(s) are inserted into the text field.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10720984
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10721029
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721158
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
 

Author Comment

by:TrainMan
ID: 10725112
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
 

Author Comment

by:TrainMan
ID: 10766569
Sorry everyone.  It  was my intent to split the point to the 3 of you.

TrainMan
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10767755
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 68
mapBully challenge 6 93
JList custom Cell Renderer refresh 15 43
maven project error 5 24
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now