Solved

How to restrict Charachters in JTextbox

Posted on 2003-12-12
5
603 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

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
nestparen challenge 4 56
firstChar challenge 13 83
wordmultiple challenge 12 89
Eclipse Neon and jdk 1.8.0 11 66
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

708 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

21 Experts available now in Live!

Get 1:1 Help Now