wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Event Handling: What is the best way? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Event Handling: What is the best way?" Watch "Event Handling: What is the best way?" New topic
Author

Event Handling: What is the best way?

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Hey all,

I am creating a software which has a JMenu. In one of its submenus, there are 20 JMenuItems which will be doing the exact same work, passing an "File" as a parameter to another method. The only difference is, these 20 menu items pass 20 files. So, this is my question;

Do I need to create 20 sub classes for Event Handling? Or, isn't it easy and OK to just use one sub class as an Event handler, and use If else statements to identify the caller?
I am asking this because I feel creating 20 sub classes for doing exact same work which has a small difference is time consuming. Please help..


Are you better than me? Then please show me my mistakes..
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Subclass once. Pass the "file" in the constructor. Refer to this variable while executing the action.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Maneesh Godbole wrote:Subclass once. Pass the "file" in the constructor. Refer to this variable while executing the action.


wow! That was awesome! Thanks a lot!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Event Handling: What is the best way?