my dog learned polymorphism*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes O-O event handling.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "O-O event handling.." Watch "O-O event handling.." New topic
Author

O-O event handling..

Zafer Abu saeed
Ranch Hand

Joined: Mar 28, 2004
Posts: 40
Hello Everybody..
Do any body know the "preferred" way to do "object oriented event handling" ?
I read that using a single event handler and a switch statement is not an object-oriented .. (Actually this makes sense).
I think using a single Action for each event will be a good solution,
but I'm worry about two things:
1) This may result in a large number of classes (either inner or normal).
2) The Javadoc for javax.swing.Action interface states:
Note that Action implementations tend to be more expensive in terms of storage than a typical ActionListener, which does not offer the benefits of centralized control of functionality and broadcast of property changes. For this reason, you should take care to only use Actions where their benefits are desired, and use simple ActionListeners elsewhere.
Do you have a better solution ?
Thanks.
[ April 05, 2004: Message edited by: Zafer Abu saeed ]

Thanks,<br /> Zafer<br /> SCJP, SCWCD
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Hi Zafer.
Yes having one ActionListener with a big switch case is not OO design at its best.
However, you can still have one class be the "handler" or "controller" of the actions, as in the Controller in Model View Controller.
The way that I handle this design is by creating "Hook" methods in your View.
Meaning you view only has code that shows the visual aspects of the screen, and then methods that allow an outside class to call passing it an ActionListener, to add to a particular component on the view.
Here is an example from my view.

Then in the Controller you would have a method that hooks into each call in the view
here is an example of this code

As you can see I use an anonymous ActionListener class to call the view "frontEnd" hook method from above. The searchFlight() method call is a method in the Controller that only handles the Action from the search button in the View.
Hope that helps.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Zafer Abu saeed
Ranch Hand

Joined: Mar 28, 2004
Posts: 40
Thanks Mark,
I think passing an anonymous class as the handler to the view (using for example View.searchAction(ActionListener)) will not fit my case. I think that using Actions will be more centralized..
What if I have a Search button, Menu Item and toolbar-button? then if I need to disable the search action I have to disable all this three components.
Using javax.swing.Action will cause the three components to be disabled if the associated Action is disabled.
Specifically I have two issues:
1) I have a JFrame which has a Menu Item and a Button for each action, I think using javax.swing.Action is appropriate here,
so should I implement an inner class for each Action and put all the logic inside it?
or should I make each inner class invoke a method from a centralized Controller class?
or use a design that is completely different ?

2) If I have two buttons to perform the same functionality, one button is on the main window (JFrame) and the other is on a JDialog, where I should place the logic that will be performed when one of these two buttons is clicked?
Guys, please advise...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: O-O event handling..
 
Similar Threads
SCJP2 1.2 or SCJP2 1.4
Whats upgraded in SCJP 1.6 Upgrade (310-066) and which book is upgraded accordingly?
switch statement
Swing
Anonymous class