tags:

views:

21

answers:

1

I create GUI in Netbeans Form Editor and want to change an auto-generated signature of one method, namely to add throws to it. How to do it?

For instance, I have
private void btOpenFileActionPerformed(java.awt.event.ActionEvent evt) {}

And want to make it
private void btOpenFileActionPerformed(java.awt.event.ActionEvent evt) throws AssertionError{}

As the method signature is auto-generated, I cannot change it manually.

+2  A: 

You cannot change the signature of the action-performed event method in any way other than changing the name.

What are your intentions for the exception once it gets thrown from your event method? The exception is going to be passed up to the auto-generated action listener, and that action listener cannot pass it along because that would be a violation of the ActionListener#actionPerformed contract.

If you want to throw an exception from your event method, you have two options. The first is to have the auto-generated event method call the exception-throwing event method. The second is to manually create an ActionListener and attach that to the component rather than relying on the automatic generation. In either case, you will need to handle the exception: a checked exception cannot be passed back from the ActionListener.

Matthew T. Staebler