Solved

How to fix the size of the text in a JTextField

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example 2 38
spring maven example issues 3 44
dao vs facade design patterns 2 38
Java Inheritance super keyword use 8 38
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

726 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