File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Design question." Watch "Design question." New topic

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: 862
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. - 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

[ 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.

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

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:
subject: Design question.
jQuery in Action, 3rd edition