?
Solved

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

Posted on 2001-08-06
5
Medium Priority
?
240 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
[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
  • 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 100 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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 …
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…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

801 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