Solved

Limit the word in textfield

Posted on 1998-07-06
6
275 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 85
starter POM and spring-boot-starter,  spring-boot-web 2 57
map related example 6 49
java stored proc example 9 32
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

822 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