Solved

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

Posted on 2001-08-06
5
213 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …

770 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