I am trying to change the value in a jTextField when I press the UP and DOWN arrow keys on the keyboard. When UP is pressed the value goes up by a set amount and when DOWN is pressed it goes down.
This is what I so far:
private void engineTxtKeyPressed(java.awt.event.KeyEvent evt) {
try{
int code = evt.getKeyCode();
double value = Double.parseDouble(engineTxt.getText());
if (code == KeyEvent.VK_UP) {
value = value + 25.00;
if ((value) > 100.00) {
value = 100.00;
engineTxt.setText(String.format("%.2f", value));
}
}
else if(code == KeyEvent.VK_DOWN){
value = value - 25.00;
if ((value) < 0.00) {
value = 0.00;
engineTxt.setText(String.format("%.2f", value));
}
}
} catch(Exception e){
System.out.println("error");
}
}
Before I put the try/catch in there, the error I got was:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "0,00"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
The list goes all the way down for many lines, basically it would flag anything with parsing and formats throughout the process.
The thing is that I have the same parsing for another 10 TextFields and there is no problem, but this is the only one with a keyEvent. After adding the try/catch I get my sout() message every time i press a key and the rest of the program continues just fine.