Here is one way to do it. It can be improved with an array.
public boolean onlyNumbers(KeyEvent evt) {
char c = evt.getKeyChar();
boolean returnValue;
returnValue = !(Character.isDigit(c));
returnValue &= !(c == KeyEvent.VK_BACK_SPACE);
returnValue &= !(c == KeyEvent.VK_DELETE);
returnValue &= !(c == KeyEvent.VK_END);
returnValue &= !(c == KeyEvent.VK_HOME);
returnValue |= (c == KeyEvent.VK_PAGE_UP);
returnValue |= (c == KeyEvent.VK_PAGE_DOWN);
returnValue |= (c == KeyEvent.VK_INSERT);
if(returnValue)
{
evt.consume();
returnValue = !returnValue;
}
return returnValue;
}
The first five and three last jobs can be compressed in their respective groups to two general purposes, but this will come down to your coding standards.
source
share