0

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:

enter image description here

If you click the Select Tables button, you get this pop up:

enter image description here

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.

13
  • 4
    Yes and no. Your views should be focused on a single task each having their own controller and model. When I say "view", I don't mean window, I mean each individual component group, which has a specific responsibility, like showing a list of the tables and the view responsible for showing the meta data of a table and the view responsible for showing the contents, each are responsible for a specific job and represent a individual mvc. You then begin grouping theses specific mcv's into larger, modular mcv's, building up the ui and its functionality Commented Dec 29, 2015 at 21:43
  • 3
    You should also be focusing on using interfaces over implementation, this means you focus on the details of the contracts between the components of the mvc over its physical implementation, making easier to change the implementations and the more flexible Commented Dec 29, 2015 at 21:46
  • 2
    Deciding what to pass to each layer can cause a lot of stress, in a "traditional" mvc, the model doesn't know about the view and the view doesn't know about the model, they are bridged by the controller. Obviously this isn't the way Swing works, where the model is shared between the view and controller, thus can sometimes make it difficult to implement a pure mvc in Swing. Each approach has pros and cons, I tend to mix them a little, containing the swing based components into a self contained view, then using the views interface, define what the view inputs/outputs as well as events Commented Dec 29, 2015 at 21:53
  • 2
    I've done a few decisions on the subject which might help, have a look at this, this, this, this for starters Commented Dec 29, 2015 at 21:57
  • 1
    And a extended example which demonstrates much of what I've discusses above Commented Dec 29, 2015 at 22:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.