Solved

How to restrict Charachters in JTextbox

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 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