Rosh Donniet
Rosh Donniet

Reputation: 418

How to properly design an MV application in Java Swing?

I'm trying to design an MV (Model-ViewController) application in Java swing. I'm having trouble with assigning the correct model to each ViewController.

Here's my current design (not the actual names) : class diagram

The idea is that I have two views : the Gui and a Midi Device.

Each view has its corresponding model. However, a part of the Gui (ButtonBar) needs to access the midi device model.

For now, my solution is to pass the MidiDevice model to the MainWindow's constructor, then let it pass it down to the MyPanel constructor, then to the ButtonBar... To me, that smells bad design and future complications.

What better way do you suggest ?

Upvotes: 1

Views: 109

Answers (1)

trashgod
trashgod

Reputation: 205885

One approach is to let the device view and/or model export the Action instances required by the ButtonBar. In this simple example, the view exports actions used to control the display itself. In the more complex examples cited here, actions provided by EditorKit subclasses operate on the Document model used by text components; listening views update themselves in response. This example creates a JToolBar using such actions. Finally, JHotDraw, cited here, generates actions dynamically for use in a tool bar, as discussed here.

Upvotes: 2

Related Questions