Ngonidzashe Munyikwa

Greenhorn
+ Follow
since Oct 28, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ngonidzashe Munyikwa



Thanks Matt. Worked like a charm. I made a slight modification to your code to make sure One point character alone is entered , the resulting code is below


private void jTextField1(java.awt.event.KeyEvent evt) {

char c = evt.getKeyChar(); // Get the typed character

// Don't ignore backspace or delete
if (c != KeyEvent.VK_BACK_SPACE && c != KeyEvent.VK_DELETE) {
// If the key was not a number then discard it (this is a sloppy way to check)
if (!(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9')) {

//if point character typed increase numberofpointcharacters by one
if ((c == '.')) {

// If the input string already contains a decimal point, don't
// do anything to it.
if (jTextField1.getText().indexOf(".") < 0)
{
// don't consume it
}
else
{
evt.consume(); // Ignore this key
return;
}

}
else{

evt.consume(); // Ignore this key
}

}
}

}


12 years ago