wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes mvc - adding views to main GUI frame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "mvc - adding views to main GUI frame" Watch "mvc - adding views to main GUI frame" New topic

mvc - adding views to main GUI frame

Jonathan Janisch

Joined: Mar 17, 2007
Posts: 24
Hi. I just read Head First Design Patterns (it's an awesome book!). In the chapter on MVC, it says the controller creates the view. This works great for their example, because their view is the main JFrame. In my case, my views are components I want to add to my existing JFrame.

I have the EXACT same problem as this guy and oddly enough he also just read the same book:


but I don't necessarily understand the answer he was given.

So, since he's better at explaining it, I'm going to quote his post:


If I have a a couple of different Views which are created by a couple of different controllers what would be the best way of adding these to a main frame.

For example

View1 extends JPanel
View1Controller creates it.

View2 extends JPanel
View2 Controller creates it.

MainFrame extends JFrame

currently I have the controllers returning the View that they create so I can add them like so


This however does not sit right with me, though I'm not sure why.

Does anybody have any thoughts or insights on the above.

In addition to his question, should my main JFrame be creating controller1 and controller2? Or should this be done in non-gui code and passed into the MainFrame constructor?


or should I do this:

I believe the first approach is better as long as the MainFrame is allowed to create the controllers. With the second approach, if I have 10 controllers, these all have to be passed to the MainFrame constructor. Or are none of these approaches correct?

I'm open to any advice. I feel like I'm closer than ever to understanding MVC thanks to HFDP, so thanks if you were involved with that book.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38045
Welcome to the Ranch

Is this recent article from the Java Developers' Series any use to you?
Jonathan Janisch

Joined: Mar 17, 2007
Posts: 24

That did help. I'm not quite sure if I like their modified MVC approach, but I can use the part where they add the views to the controller.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38045
You're welcome.
I agree. Here's the link: http://aspose.com/file-tools
subject: mvc - adding views to main GUI frame
Similar Threads
Multithumb slider
How to refresh View in MVC architecture.
Problems in working with local home interfaces
key conflicts
Scrollable JPopupmenu