• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Design question.

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bob,

Please ignore my previous posting.

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic