Solved

Limit the word in textfield

Posted on 1998-07-06
6
264 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
ID: 1224732
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
ID: 1224733
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
ID: 1224734
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:drasin
ID: 1224735
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
ID: 1224736
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
ID: 1224737
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

920 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

14 Experts available now in Live!

Get 1:1 Help Now