Try introducing the Apache's "commons Lang" library into your project and for your last line you can do
int aInt = 0;
if(StringUtils.isNotBlank(aString) && StringUtils.isNumeric(aString) ){
aInt = Integer.parseInt(aString);
}
edit: Not sure why the downvote. The JtextField will take any string. If the text field is listening on each key press, every non-numeric value (including blank) that is entered will generate the NumberFormatException. Best to check if it is Numeric before doing anything with the new value.
edit2: As per Thomas' comments below. I ran a test to compare the try/catch vs the StringUtils way of solving this issue. The test was ran 5million times for each. The average time for the try/catch was 21 seconds. The average time for the StringUtils was 8 seconds. So using StringUtils for heavy load is considerably faster. If the load on the code is small you will notice little to no difference. The test ran was
try{
result = Integer.parseInt(num);
}catch(NumberFormatException ex){
result = -1;
}
vs
if(StringUtils.isNotBlank(num) && StringUtils.isNumeric(num)){
result = Integer.parseInt(num);
}else{
result = -1;
}
each loop through generated a new random string of 10 digits to avoid any optimization in the loops on the if statement. This added 6-7 seconds of overhead.