• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

Limit the word in textfield

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
hcyu
Asked:
hcyu
  • 4
  • 2
1 Solution
 
drasinCommented:
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
 
drasinCommented:
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
 
hcyuAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
drasinCommented:
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
 
drasinCommented:
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
 
hcyuAuthor Commented:
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

Independent Software Vendors: 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!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now