This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Design question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Design question." Watch "Design question." New topic
Author

Design question.

Bob Green
Ranch Hand

Joined: Feb 29, 2004
Posts: 93
I need to get some ideas from the experts. I have a JTree with three nodes; every time a node is selected, it has to execute some task. For instance, if node A is selected, action A is invoked. If node B is selected, action B is called. You get the idea. I am wondering is there any design pattern that I can use to implement this scenario. I don't want to have a bunch of 'if' statements for checking the node to determine what action needs to call.
If any one knows of anything that can help me, pls. let me know.

Thanks in advance!
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
Each of your nodes could implement the same interface (call it Node), and this interface can have a method on it (call it action()). That way all code that deals with Nodes can treat the the same and simply call the action() method on each Node and have it take the appropriate action.



http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I don't do much Swing so don't pretend to be a Swing Sophisticate. I did a few programs with one ActionListener for virtually all events, and a registered command for every source.

There was a lot of code like

Opinions?
[ September 06, 2004: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
Hi Bob,

Please ignore my previous posting.



SCJP,SCWCD,SCBCD,SCEA Part I
Nikhil Vasaikar
Ranch Hand

Joined: Aug 18, 2004
Posts: 56
Hi Bob,

I have created a tree and the events on which are actioned using polymorphism. I suggest you go through the code and find out if there is anything that might help you.

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Sujatha Kumar:
Please ignore my previous posting.


You can delete your own posts - just click on the edit/delete icon of the post (the second from right on the top).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design question.
 
Similar Threads
deletion of Node in JTree
Setting selected node of a rich:tree dynamically
struts dynamic menu question
Node Selection in JTree
Event on selecing the same node in a JTree