*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Opening the main application window & MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Opening the main application window & MVC" Watch "Opening the main application window & MVC" New topic
Author

Opening the main application window & MVC

Chris Zaremba
Ranch Hand

Joined: Nov 22, 2010
Posts: 54

I've seen lots of examples and opinions on how to implement MVC but I've decide to use an implementation described here Modified MVC. Basically the controller sits between the model and view and the later two do not communicate directly with each other. So my constructor looks something like this



In addition to this my public void main(String[] args) method is in launcher class that deals with the command line arguments and this will launch the main window. What would be the best way to open the main window? Should the launcher creater the model, view and finally the controller instances? This couples the launcher class to each of the three MVC components. If the application was expanded and more windows were added this would result in each parent window knowing about all three child window components. This doesn't seem right to me. The only way I can see to stop this is to have a fourth class that create the MVC components and the launcher only calls this but I've not really seen this in any examples though. Andrews book has the main method in the view which makes that component the one that's in charge which is not inline with the implmentation I'm going for.


SCJA, OCPJP, OCMJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

Hi Chris,

I simply evaluated the command line arguments in my executable main class and then simply called something like new MainWindow(args);. The constructor of the MainWindow will perform the necessary initializations.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Chris Zaremba
Ranch Hand

Joined: Nov 22, 2010
Posts: 54

Hi Roel,

Would I be right in saying that MainWindow is the view part of the MVC? Although many example I've seen do it that way, I was under the impression that it's the controller that should be running the show.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

My MainWindow is indeed the V. I have a controller (C) who communicates with the business service (M). C tries to get data from M and passes it to V and V renders it so the user can see the results of his search
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Chris!

Basically the controller sits between the model and view and the later two do not communicate directly with each other.


Well, that's how things work in regular MVC...

What would be the best way to open the main window?


Well champ, I myself didn't have any class called Controller or something because in Swing ActionListeners can be faced as controllers. So, given the command line argument, I open the proper configuration dialog. Then, based on the user's input, I instantiate the MainWindow inside the ActionListener that listens to the OK button of the launch dialog.

And finally, please take a look here. I think it might be helpful!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Chris Zaremba
Ranch Hand

Joined: Nov 22, 2010
Posts: 54

Thanks chaps. I've decided to go with a normal MVC pattern where there the model does have a reference to the view and have stuck with the Model, View and Controller names. Now I've got my head around it, it actually makes things quite simple. The start up dialogue will come later but for now I have do records displaying and the search working so I'm a happy man
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Opening the main application window & MVC
 
Similar Threads
To Dave Macarthy, working of JTable
Is this MVC ?
Forwarding data changes in MVC
Adding an action listener to an entire class
MVC in Swing for a modularised View