Solved

How do I limit the No. of Chars in the TextField ?

Posted on 2001-08-06
5
195 Views
Last Modified: 2010-03-31
The database field size is 5 chars. But, user can enter more than 5 chars in the text field in an applet. I can do edit check and display a message. But, I want to limit entering the 6th char. Is there any way ?

I can use only AWT components.

Thanks.
0
Comment
Question by:UU1
  • 3
5 Comments
 
LVL 1

Expert Comment

by:dviji
ID: 6357405
Hi UU1!
     We can do it by using the keyListener. In the KeyListener's  KeyTyped() method you can do your validation. Here check the length of the string in the textfield using textfield.getText() if it is greaterthan 5 then do textfield.setText(oldText). For TextField if you create your own class " FilteredTextField which extends TextField" and implement the KeyListener. So that you can have your own properties and validation in thid class. It will be resuable also.

...dviji
0
 
LVL 15

Accepted Solution

by:
a.marsh earned 25 total points
ID: 6357439
I've just put this together for you:

import java.awt.*;
import java.awt.event.*;

class AntTest{
     Frame mainFrame;
     Panel mainPanel;
     MaxTextField textField;

     public AntTest(){
       mainFrame = new Frame("MaxTextField Test");
       mainFrame.setSize(200,100);

       mainPanel = new Panel();

       textField = new MaxTextField(10, 6);

       mainPanel.add(textField);

        mainFrame.add(mainPanel);

       mainFrame.show();
     }

     public static void main(String[] args){
       AntTest myAntTest = new AntTest();
     }
}

class MaxTextField extends TextField implements KeyListener{

     private int maxLength;

     public MaxTextField(int cols, int maxLength){
       super(cols);
       this.maxLength = maxLength;
       this.addKeyListener(this);
     }

     public void keyPressed(KeyEvent ke){
       String currText = getText();
       if(currText.length() >= maxLength && ke.getKeyCode() != ke.VK_BACK_SPACE){
         ke.setKeyCode(ke.VK_CANCEL);
       }
     }
     public void keyTyped(KeyEvent ke){
     }
     public void keyReleased(KeyEvent ke){
     }
}


Compile it and run it to see it in action.

:o)

Ant
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6357440
Sorry dviji, I've only just seen your comment - great minds think alike though!

:o)

Ant
0
 

Author Comment

by:UU1
ID: 6359880
Thanks March. Appreciate.
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6360533
Glad to help.

:o)

Ant
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changeXy challenge 13 56
json example 39 111
what is a "java.lang.System Property"   ? 20 35
Java DateChooser? 2 5
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

13 Experts available now in Live!

Get 1:1 Help Now