?
Solved

How to fix the size of the text in a JTextField

Posted on 2001-06-07
2
Medium Priority
?
636 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
sontnvn earned 600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

765 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