Solved

Limit the word in textfield

Posted on 1998-07-06
6
282 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
table example 4 32
web application structure 18 103
jboss wildfly 10.1 10 251
Java Restore security prompts not working 10 73
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 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