Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2001-08-06
5
Medium Priority
?
245 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

Industry Leaders: 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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This video teaches viewers about errors in exception handling.
Suggested Courses

636 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