Solved

How to fix the size of the text in a JTextField

Posted on 2001-06-07
2
630 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

816 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

11 Experts available now in Live!

Get 1:1 Help Now