I have a program that allows users to open multiple databases, select tables from them, and then select the columns in the tables. So my GUI looks something like this:
If you click the Select Tables button, you get this pop up:
My MainView contains the menu bar, the toolbar, the JPanel that contains a CardLayout for switching between databases. I have a MainController for the main frame that controls the MainView and the MainModel. The MainModel has a method that creates a connection to an existing database:
public void connectDatabase() {
DatabaseModel dbModel = model.connectDB();
DatabaseView dbView;
DatabaseController dbController;
if (dbModel != null) {
dbView = new DatabaseView(dbModel.getName());
dbController = new DatabaseController(dbModel, dbView);
view.addDBchoice(dbModel.getName(), dbView);
dbControllers.add(dbController);
}
}
A DatabaseModel is just a representation of the database. It contains the Connection, a static list of all its tables' names (Strings), and a dynamic list of Table objects (details below).
The DatabaseView contains a Select Tables button, and a JTabbedPane (details below).
Now, once a Database connection is created, the user will be able to select which tables from that database would they like to open. The DatabaseController is the one that can open the JFrame (shown above) that allows the user to select tables they want. After selecting the tables, this code inside my DatabaseController creates Table models, views, and controllers, for each of the selected tables:
for(int i = 0; i < indices.size(); i ++) {
tableModel = model.getTableModel(selectedValues.get(indices.get(i)).toString());
tableView = new TableView();
tableController = new TableController(tableModel, tableView);
tableView.addController(tableController);
model.selectTable(tableModel);
view.addTableTab(tableView);
}
Each TableView (JPanel that contains a dual list box) is added to the JTabbedPane (as shown above), and each TableModel is added to the dynamic list of Table objects I mentioned above. A TableModel is just a representation of a table. It contains the names of its columns.
Now, my main question is: is this the correct way to implement MVC in Java?
For example, in my program, the DatabaseController controls the TableControllers, and later on, I will have ColumnController, ColumnModel, and ColumnView classes. A TableController then can control the ColumnControllers. Is this a good idea?
Also, should the parameter for View objects be its Controller? If that's the case then should the Controller only have the Model as a parameter?
Any other suggestions/criticisms/opinions are welcome.