Solved

How to fix the size of the text in a JTextField

Posted on 2001-06-07
2
628 Views
Last Modified: 2010-05-18
Hi,
How to fix the size of the text in a JTextField.I want to fix the text size in JTextField,if user enters beyond that limit , some beep sound has to come.Is there any method, send me the sample code.Thanks in advance.
rgds
-Raman
0
Comment
Question by:pv_raman
2 Comments
 

Accepted Solution

by:
sontnvn earned 200 total points
ID: 6166368
Hi pv raman,
You can create an inner class below to your class

class MyDocument extends PlainDocument {
     int fixLength = 0;
     TextField textField = null;
     public MyDOcument(TextField txtField,
                         int fixValue){
          this.fixLenght = fixValue;
          this.textField = txtField;
     }
     public void insertString(int offs,
               String str, AttributeSet a)
     throws BadLocationException{
          if (str == null){
                       return;
                }
                String oldValue =                               textField.getText();
                if (oldValue.length() >= fixLenght)
                     return;
                super.insertString(offs, str, a);
         }
}
and then whenever you want to set textfield just do follow:
   
     yourTextField.setDocument(
          new MyDocument(yourTextField,30));
so this text field will recieve max 30 letter.
good luck.
0
 

Author Comment

by:pv_raman
ID: 6190774
Accepted.
-raman
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

707 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