Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Design question.

 
Bob Green
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sujatha Kumar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bob,

Please ignore my previous posting.

 
Nikhil Vasaikar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic