6

I have a GUI with nested panels(tabbed with nested panels and etc). I need to pass domain object to deeply nested panel. I can think of two ways:

  • Instantiate all gui objects in one place, like frame class. That would make passing domain objects dead simple, but Frame class will be huge and hardly maintanable.

  • Each panel has its own class, where we instantiate and layout its
    components. Now its easy to maintain and classes are clean, but how
    do I pass down the chain my domain objects? I dont want to chain-pass them through constructors of panels that shouldn't even know their
    existance. And top level panels would have a ton of these objects to start with.

Niether way seems like a soulution. How do you usually aproach this?

Boris Mikhaylov
  • 329
  • 1
  • 12
  • An interesting question, and I'm not sure how professionals solve this, but one thought would be that the domain object (whatever that is) wouldn't be passed directly to the GUI but to its model, and that the GUI would have listeners attached to react to changes in the model. Thanks for the question. I'm going to be listening to this thread to see what the pros have to say. – Hovercraft Full Of Eels Oct 22 '11 at 22:57
  • 4
    This link may prove useful: [Java GUI Development: Reintroducing MVC](http://www.theserverside.com/news/1364562/Java-GUI-Development-Reintroducing-MVC). I know that I found it fascinating! – Hovercraft Full Of Eels Oct 23 '11 at 01:21
  • MVC is the correct guiding principle: each view should hold a reference to the model(s) to which it listens. IMO, there's not enough detail in the question to say much more. See also this [answer](http://stackoverflow.com/questions/3066590/gui-problem-after-rewriting-to-mvc/3072979#3072979). – trashgod Oct 23 '11 at 03:56
  • @HovercraftFullOfEels - you should post that link as an answer. Very enlightening. Wish I had known that back when I first started to deal with Swing. – Chris Aldrich Oct 25 '11 at 13:50

2 Answers2

2

When I put together a Java Swing GUI, I have a data model for each major GUI element. Note that this isn't an MVC pattern. This is more like a local MV pattern. If you want, you can consider GUI element listeners as the "controller".

Each panel has its own class, where we instantiate and layout its components. Now its easy to maintain and classes are clean, but how do I pass down the chain my domain objects?

You have the right idea, although you shouldn't have to do much passing.

My JFrame (or JApplet) will have an associated model class of global type fields. An instance of this model class will usually be passed along to the children elements. This is to allow children elements to react properly when a menu option is selected (as an example)

My JPanel(s) will have an associated model class that maintains the state of text or button children elements.

More complicated children elements, like a JList or a JTree, already have an associated data model. I will probably wrap these associated data models into the JPanel model class for convenience.

The children elements will trigger some sort of selection or action listener. Some of these listeners might need access to model classes besides the model class associated with the parent. In this case, you're going to have to pass the instances of your model classes to the listeners.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
0

This is sort of a Chain of Responsibility pattern. What I would do is have something that creates a map with all of your display objects in it and pass it from constructor to constructor. That way every instance can take what it needs from the map without caring what else is there.

Thom
  • 14,013
  • 25
  • 105
  • 185