Solved

How to fix the size of the text in a JTextField

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
Error in @AspectJ Based AOP with Spring 2 18
Convert from a json string array to a Java object 3 51
java imports not found 4 21
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…
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 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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

808 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