I have a toolbar with a combobox. When the item changes on the combobox, it makes a call to the draw panel (which the toolbar has a reference to) to tell the draw panel to change the shape type. Now, the the draw panel has a popup menu that changes the shape type aswell. Therefore, when the popup menu changes the shape type, the combobox needs to be notified as well. The only way I can think of it is for the draw panel to have a reference to the toolbar, but I do not like the idea of a circular dependency. Is there a better way?
Thanks
According to the model-view-controller pattern which is commonly considered the best approach for GUI coding, you’d have a model, a view and a controller. In this case, the underlying model would be some value corresponding to the state of the combo box and what the popup menu shows. The views are the combo box and popup menu. They are both also controllers since they can alter the model’s state.
So the “clean” way to implement this is to hold the state somewhere separately and have a list of components there that should be notified when the model state changes. On a state change, the list should be iterated and the components in it should have their view updated. The combo box and menu entry should be registered as such “listeners”.
Now, I said that was the “clean” way, but for a simple setup as yours you don’t necessarily have to implement it this way. It can complicate things and make the code less legible. On the other hand, doing it the MVC way will make future changes easier, like adding new views/controllers. Doing that without MVC would result in more and more components requiring knowledge of each other’s existence, while now it’d all be cleanly encapsulated in a model.