Solved

How to restrict Charachters in JTextbox

Posted on 2003-12-12
5
616 Views
Last Modified: 2009-12-16
Hello everyone,
     I cannot seem to find a way how to restrict the number of charachters in a JTextbox. I know that there is this Constructor where you can pass the COLUMN size but that doesnt work. Java 1.4 has this JFormattedTextbox where you can set the mask and specify something like "####" to restrict the entries to only 4 charachters, but that has a problem. By default it puts blanks in the textfield and that is a problem.
Thanks
- Adil
0
Comment
Question by:AdilK
  • 3
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 75 total points
ID: 9929035
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9929041
That link shows you the Pre JDK1.4 way, and the after JDK1.4 way :-)

Good luck!!  

Tim
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9929047
Can't test this at the moment but try


JTextField tf = new JTextField(4);
tf.setDocument(new FourCharDoc());

.....

class FourCharDoc extends PlainDocument {
      public void insertString(int offs,String str,AttributeSet a) throws BadLocationException {
            if (getLength() < 4)
                  super.insertString(offs,str,a);
      }
      
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9929065
Didn't see that link before post sorry. Use the code they give, not mine.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9929074
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 80
JDeveloper 12c for 32 bit 4 71
Apps blocked by Java 9 63
web services creation SOAP vs REST 5 20
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

16 Experts available now in Live!

Get 1:1 Help Now