Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing Actions in a Swing Application?

My current application has a JFrame with about 15 actions stored as fields within the JFrame. Each of the actions is an anonymous class and some of them are pretty long.

Is it common to break actions into their own classes possibly within a sub-package called actions?

If not, how's this complexity usually tamed?

Thanks

like image 247
Allain Lalonde Avatar asked Jan 15 '09 19:01

Allain Lalonde


1 Answers

If it is possible that your actions could be reusable (e.g., from keyboard shortcuts, other menus, other dialogs, etc.) and especially if they can work directly on the underlying model (rather than on the UI), then it is generally better not to have them as anonymous classes.

Rather, create a separate package, and create classes for each.

Often, it also makes sense to not instantiate these directly but rather have some sort of a manager that defines constants and initializes and returns sets of actions, so that you could, for example, offer different action sets at different versions or set certain actions only for internal releases.

Finally, check whether your actions can be refactored into a class hierarchy. They often can, which saves code replication, and also helps you add robustness (e.g., check for certain conditions before letting the action execute).

like image 143
Uri Avatar answered Oct 20 '22 05:10

Uri