When I compiled the Java Swing GUI, I have a data model for each core element of the GUI. Please note that this is not an MVC pattern. This is more like a local MV pattern. If you want, you can consider the listeners of the GUI elements as a โcontrollerโ.
Each panel has its own class, where we create and assemble its components. Now itโs easy to maintain and the classes are clean, but how do I pass the chain of objects of my domain?
You have the right idea, although you do not need to go through much.
My JFrame (or JApplet ) will have an associated model class of global field types. An instance of this model class is usually passed along with children. This allows children's items to respond appropriately when selecting a menu option (as an example)
My JPanel(s) will have an associated model class that maintains the state of text or child elements.
More complex children, such as JList or JTree , already have an associated data model. I will probably be porting these related data models to the JPanel model class for convenience.
The children elements will cause some kind of choice or action listener. Some of these listeners may require access to model classes other than the model class associated with the parent. In this case, you will have to pass instances of your model classes to listeners.
source share