You're trying to catch the exception on the method that adds the action listener, not the method that actually tries to parse the string as an integer. If you wrap that in the try / catch block, then it should work as you expect:
o88.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
int a = 0;
a = Integer.parseInt(t1.getText());
}
catch(NumberFormatException ex) {
System.out.println( e.getMessage());
}
}
});
Note that if this is a real world program you'll want to pop up a dialog (probably using JOptionPane
) rather than print to the console, and you'll want to print something much more human readable then the exception error message.