A Java Model View Controller example (Part 2)

<< Return to Part 1 of my Java MVC Example

MVC Controller - our MainController class

As its name implies, the MainController is the main controller/coordinator of our MVC application. It creates the other controllers, and in many ways then turns control over to these other controllers. One of the controllers it creates is the ProcessController.

If we looked at all of the MainController source code, we'd see that it contains the main method for my application. It does the database connection stuff it needs to do, constructs the main frame (JFrame) of the application, and displays a login dialog to the user.

Besides doing those things, it also knows about all of its sub-controllers, so it has lines of declaration code near the top of the class that look like this:

public class MainController
{
  EntityController entityController;
  ProcessGroupController processGroupController;
  ProcessController processController;
  ReportsController reportsController;
  
  // more code ...

Later on in the MainController class there are constructor lines of code that look like this:

entityController = new EntityController(this);
processGroupController = new ProcessGroupController(this);
processController = new ProcessController(this);
reportsController = new ReportsController(this);

As you can see from that code, there are two things happening here:

  1. I'm constructing each controller.
  2. I'm giving each controller a reference back to the MainController. This is useful for making various callbacks.

MVC Controller - our ProcessController class

The ProcessController class is the controller for all things related to FPA processes. It has references to all of the Process GUI elements referred to earlier, such as these:

MainProcessPanel mainProcessPanel;
MainProcessTable mainProcessTable;
MainProcessTableModel mainProcessTableModel;
EditProcessDialog editProcessDialog;
ProcessReportPanel processReportPanel;
ProcessDetailReportPanel processDetailReportPanel;

One important thing to note here when working with a JTable is that you often maintain your table data in a Collection or List, and that data is then wrapped by a TableModel. For example, in my application I will maintain a list of Process objects like this:

List<Process> processes = new ArrayList<Process>();

and then after that I will tell my TableModel that it is a wrapper around this list of processes, perhaps in its constructor, like this:

// wrap a tablemodel around my list
mainProcessTableModel = new MainProcessTableModel(processes);

Again, the TableModel is just an adapter class that is meant to glue your data model to a JTable.

Besides being aware of these GUI elements, the ProcessController also handles the Process-related events for the application. It handles these events through many smaller granular Java Actions, with one such action being named AddProcessAction, so you'll also find code like this in the ProcessController class:

// declare it at the top of the class
AddProcessAction addProcessAction;

and this:

// instantiate it later on
addProcessAction = new AddProcessAction(this);

This action listens to the "Add Process" button, and is triggered whenever the user presses that button. In turn, this action calls back to the ProcessController and says something like, "Hey, we just got an 'Add Process' action, and you need to deal with it." The ProcessController receives that event, and invokes a Java method named startAddProcessAction, which in this case means that it constructs and displays a the EditProcessDialog (in 'add' mode).

<< Return to Part 1 of my Java MVC Example

Continue to Part 3 of my Java MVC Example >>

 

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.