简体   繁体   中英

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) : 类图

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 ?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM