I am developing my GUI according to the MVC pattern:
-GUIview: Swing components (JFrame and several JTables). -GUIcontroller: listeners (added here, and defined here in Inner classes) -GUImodel: modify and store data, firing change-events.
Changes in the model are passed to the view through the controller (and not directly), like in this example.
I have also written different customized JTableModels (extending AbstractTableModel) for the different JTables contained in the View class. All the JTableModels are defined in different classes within the package "GUImodel". Each JTableModel defines an ArrayList and a few methods to manipulate the ArrayList.
According to the MVC guidelines, the Model should know nothing about the view. In fact, the main() method is defined as follows:
GUImodel model = new GUImodel();
GUIcontroller controller = new GUIcontroller();
GUIview view = new GUIview(controller, model);
controller.addView(view);
controller.addModel(model);
view.setVisible(true);
controller.addControllerListerners();
My problem is: When I am executing a method within the GUImodel (for example because a JButton has been pressed and I need to load data from an external file), I need to modify some JTableModels (to add data/rows to its ArrayList) and get the changes reflected in the JTable. My first idea would be:
ArrayList newArrayList = fileLoader(filePath); //create ArrayList and load info
guiView.getTable1Model().updateArrayList(newArrayList); //update JTableModel ArrayList
However, this approach is not valid, since GUImodel should be totally independent of GUIview.
Any idea?