MVC Adding various views to a Main Frame

Hi,

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

getContentPane().add(controller1.getView1());

getContentPane().add(controller2.getView2());

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

Does anybody have any thoughts or insights on the above.

Thanks

Jon

[710 byte] By [JonlWrighta] at [2007-11-26 19:01:37]
# 1
Your views probably shouldn't extend JPanel.Why are you creating them inside Controllers?
es5f2000a at 2007-7-9 20:46:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Should you not extend JPanel?

Would I then extend JFrame for each view?

I'm creating the Views from within the controller due to the fact the HeadFirst Design pattern book did.

Though from looking at the current design I could probably easily take them out.

Message was edited by:

JonlWright

JonlWrighta at 2007-7-9 20:46:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

For views, I prefer to use the below. You can either create a

JPanel in the constructor, add components to it, and then add the

JPanel to the Container, or you can add components directly to

the container. This way, your view is container-agnostic -- you can

add it to a dialog, frame, jpanel ...

This is also "more OO", because you're really not adding to what it

means to be a JPanel. What you're doing is the same as

extending DefaultComboBoxModel because you want to add

some Strings.

Okay, I haven't read HFDP, so I can't comment on that. I got sold

on the above way of doing things by some forum regulars a while

ago. I have no experience with using controllers

as factories for the view. Seems to me like that

could be considered a violation of SRP, though.

public class MyView {

public MyView(final Container container) {

container.add();

}

}

...

final JDialog dialog = new JDialog();

final MyView myView = new MyView(dialog.getContentPane());

final JPanel panel = new JPanel();

final MyView myView2 = new MyView(panel);

es5f2000a at 2007-7-9 20:46:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

Thanks very much for your reply.

I'll give that a go and see what I come up with.

I don't know what the going rate for tipping dukes is so I gave you 10 ;-)

Let me know if that's me being either a tight Englishman or a extravagantAmerican :-)

Jon

Message was edited by:

JonlWright

JonlWrighta at 2007-7-9 20:46:28 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
Well, you cannot run out of Dukes - spend all you want, they'llmake more. Most regulars aren't really interested in them one wayor the other. The two strategies seem to be "throw down 10 every timeto grab the people interested in them" and "just ignore them".
es5f2000a at 2007-7-9 20:46:28 > top of Java-index,Other Topics,Patterns & OO Design...