Solved

Limit the word in textfield

Posted on 1998-07-06
6
253 Views
Last Modified: 2010-03-30
I want to know how to limit the textfield only able to display numerical value, but no other text even user types the text in the field because I need the value in the field to do some numerical calculation.
0
Comment
Question by:hcyu
  • 4
  • 2
6 Comments
 

Accepted Solution

by:
drasin earned 70 total points
Comment Utility
You need to call the Integer.decode(string(//THE_CONTENTS_OF_THE_TEXT_BOX//) method and catch the error //NumberFormatException.  Exactly where you want to do this depends somewhat on your app, but I would suggest something like the following:
      If you want to be sure that when the field is de-selected, the text in the field is properly formatted, then add a FocusListener to the text field, and put the neccesary code in the "focusLost" event [I'm assuming you know how to do this; if not feel free to contact me.].    
      Something like:
.
MyFocusListener extends FocusAdapter{
public void focusLost(FocusEvent e){
textField TF = (TextField)e.getSource();//this is your text box
String s = TF.getText().
try{
      Integer.decode(s); // Class Integer from java.lang
      }
}catch(Exception e)
      {
      //handling code as neccesary, perhaps TF.requestFocus
      //and create a dialog to inform user, whatever.
      }
Good Luck!

//the place where the textfield is constructed


*****************************************************
***** Benjamin Drasin
*****-----------------------------------------
***** drasin@mindspring.com
***** http://www.geocities.com/ResearchTriangle/9605
****************************************************
0
 

Expert Comment

by:drasin
Comment Utility
The method I showed is to check for an integer.  If you wish to allow decimals, use the class java.lang.Long and valueOf statement.
0
 

Author Comment

by:hcyu
Comment Utility
But, if the user type a non-numerical key, will the that be shown on the textfield. Actually, I want to make user impossible to type any non-numerical key in the textfield that only for calculatiion.

I know the try part, but not know what I can do in the catch part.

Thanks!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:drasin
Comment Utility
hmmm...one idea could be to use a keyListener instead, since this will catch all key events.  Then you can test the id of the KeyEvent, use getSource() on the event to address the textfield, and use getCaretPosition() find the character the user just entered, test whether it is numeric using the method I described.  This should give you one possible idea (there are others).

try{
Integer i = Ineteger.decode(s);
}catch{exception e) {//NumberFormatException
String s = TF.getText();
int cp =TF.getCaretPosition; //i think this will be the position before the event is registered.
String ss = s.subString(0, cp-1)//chop off the new letter;
TF.setText(ss);
}
hope this helps.
--Ben
0
 

Expert Comment

by:drasin
Comment Utility
I'm an idiot.  All you need to do is consume the event if it is non-numeric.  Try (in a keylistener which you have added to the text field using its addKeyListener method)

public void keyPressed (KeyEvent ke ) {
char c = ke.getKeyChar();//this is the key which was pressed
if (!(Character.isDigit(c) || (c=='.')){//allow numbers and the decimal point
ke.consume();
}
there!  that should definately do what you want.  Sometimes less is more.
bye-Ben
0
 

Author Comment

by:hcyu
Comment Utility
Yes, it does very well, but I've another small question. I can't use the arrow key to move the cursor in the textfield, and my source code is as fellow:

void textField3_keyPressed(KeyEvent e) {
    char c = e.getKeyChar();
    if(!((Character.isDigit(c)) || (c==e.VK_BACK_SPACE) || (c==e.VK_DELETE) || (c==e.VK_LEFT)
       || (c==e.VK_RIGHT)))
      e.consume();
  }

I've already included VK_LEFT and RIGHT!! Now, I can backspace and delete and limit to digital keys.
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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

772 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

16 Experts available now in Live!

Get 1:1 Help Now