I\'m just learning Java exception handling and Java in general. I\'ve made a Swing GUI where the user will enter integers into two fields and then click a radio button with an
You need to move your try-catch to the action preformed methods, right now, they are only in when Java is setting up the GUI, when the user preforms the action (actionPreformed) those methods will be called, therefore they need the try-catch, not the setup method.
private void cRadioButtonActionPerformed(ActionEvent evt) {
try {
String a = aTextField.getText();
int i = Integer.parseInt(a);
String b = bTextField.getText();
int j = Integer.parseInt(b);
int k = i * j;
cTextField.setText(Integer.toString(k));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"Error: You must enter an integer");
} catch (ArithmeticException e) {
JOptionPane.showMessageDialog(null,
"Error: You cannot divide by zero");
}
}
Add the try-catch to all the ActionPreformed methods that use this similar code, just make sure each actionPreformed method still has it's own code, just with the try-catch block around it