Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > MVC Questions

Reply
Thread Tools

MVC Questions

 
 
Miles
Guest
Posts: n/a
 
      05-28-2008

I've done a bit of reading and it appears that the newer Swing framework
implements an adaptation of MVC, but I'm trying to figure out how to utilize it.

Coming from Delphi, which doesn't provide a native MVC interface and I moved
from stock data aware controls a while ago and implemented my own passive MVC.
The way I do it now is to create a controller which in turn instantiates a Model
(class object representing an object in a database) and then creates the View
which is an inherited Delphi form with an overridden constructor used to compose
the view/form with the Model just instantiated.

I'd like to follow along the same lines in Java (or use better way if its
available) if I can by separating the GUI View from the controller and model.

Can anyone suggest further reading?

--

Miles

Note: Learning Java, coming from Delphi/ObjectPascal.
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      05-28-2008
Miles <(E-Mail Removed)> writes:
>I've done a bit of reading and it appears that the newer Swing
>framework implements an adaptation of MVC


It uses model-view separation with UI delegates for the
view-controller; this is /not/ MVC. See

http://java.sun.com/products/jfc/tsc.../architecture/

>, but I'm trying to figure out how to utilize it.


What's wrong with reading Sun's tutorial and Swing textbooks?

>I'd like to follow along the same lines in Java (or use better
>way if its available) if I can by separating the GUI View from
>the controller and model.


In Swing, the UI delegate combines the view and controller
to be one object - while the model is a separate entity.

»The first Swing prototype followed a traditional MVC
separation in which each component had a separate model
object and delegated its look-and-feel implementation to
separate view and controller objects.

We quickly discovered that this split didn't work well in
practical terms because the view and controller parts of a
component required a tight coupling«

http://java.sun.com/products/jfc/tsc.../architecture/

 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      05-28-2008
Miles wrote:
>
> I've done a bit of reading and it appears that the newer Swing framework
> implements an adaptation of MVC, but I'm trying to figure out how to
> utilize it.


_Learning Java_ should help clear this up for you. It's got a pretty
fair number of chapters on Swing, and it does discuss MVC.

>
> Coming from Delphi, which doesn't provide a native MVC interface and I
> moved from stock data aware controls a while ago and implemented my own
> passive MVC. The way I do it now is to create a controller which in turn


// Controller:

public class SomeClass {

public static void main( String ... args )
{
}
}

> instantiates a Model (class object representing an object in a database)


// Model
public class Employee {
public String first, last;
} // no getters/setters because I'm lazy

> and then creates the View which is an inherited Delphi form with an


// Form
public class MyForm extends JFrame
{
public MyForm()
{ // etc.
}
}


> overridden constructor used to compose the view/form with the Model just
> instantiated.


I'd move the model out of the form, and use "controller" methods to
separate the them. Use action listeners to bind the two.

If for example your form has two text fields for first and last names,
and an OK button, then you do something like:

public class MyForm extends JFrame
{
private JTextField fieled1, field2;
private JButton button1;

public MyForm()
{
field1 = new JTextField();
field2 = new JTextField();
button = new JButton( "OK" );
this.add( field1 ).add( field2 ).add( button );
this.pack(); // "this" actually not needed
}

public void setOkListener( ActionListener a ) {
button.setActionListener( a );
}
public String getFirst() { return field1.getText();}
public String getLast() { return field2.getText();}
}


Then in your controller you might do something like this:


public class SomeClass implements ActionListener {
private Employee emp;
private MyForm form;

public SomeClass() {
emp = new Employee();
form = new MyForm();
}

public static void main( String ... args )
{
SomeClass c = new SomeClass();
form.setOkListener( c );
form.setVisible();
}

public actionPerformed( ActionEvent e )
{
emp.first = form.getFirst();
emp.last = form.getLast();
form.dispose();
}
}


This puts your model outside of the form (but leaves the default model
for the JTextField inside the form, maybe that's what you meant. You
don't have to deal with it explicitly 99% of the time in Swing.)

Then when the OK button is clicked, the appropriate strings are
extracted through a public interface in the form, and then the form is
disposed of. (A JFrame will hang onto resources if you don't. However,
if you were going to use that form again, it might be much faster to
clear it and the re-show it, rather than construct a new one from scratch.)

Note that I really should be making the new form on the Event Dispatch
Thread. This example is so simple that there's little chance of messing
up, but get into the habit of creating your GUI on the EDT anyway. And
disposing the form from within the action listener makes me very nervous
-- I don't know if this is safe. Probably it'll work.

Also, I've compiled nothing, and the form constructor needs a little
more (like maybe a better layout manager), but the basic skeleton is
there for you to follow and improve.

 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      05-28-2008
Lew wrote:
> Mark Space wrote:
>> this.pack(); // "this" actually not needed


>
> I don't find that requirement too egregious as long as it's only for
> variables. The convention isn't needed for instance methods, which are
> already quite distinguishable as members by the very lack of an
> owner-dot prefix.


I found it difficult to understand when I was learning. You're right,
once you get used to Java, it's clear that pack() can only be an
instance method. But coming from a C background doesn't lend itself to
that sort of thinking.

Adding "this" to instance methods is a convention I use strictly for
tutorials. I assume that Miles will eventually learn enough to see that
it's unneeded. Otoh, it might be encouraging bad habits, as it's
definitely unconventional to use "this" for instance methods. I haven't
decided which way is best yet.
 
Reply With Quote
 
Arne VajhĂžj
Guest
Posts: n/a
 
      05-28-2008
Mark Space wrote:
> I found it difficult to understand when I was learning. You're right,
> once you get used to Java, it's clear that pack() can only be an
> instance method.


That was how it was before static import ...

(which can also be interpreted as that the usage of static import
should only be used when it really has benefits !)

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
No MVC Project Template after installing ASP.NET MVC 1.0 Crazy Cat ASP .Net 1 09-03-2009 08:02 PM
MVC design questions Martin Gregorie Java 25 02-13-2009 01:34 PM
WebForms X MVC? Why MVC? Give me reasons to migrate my web apps to it please. Pros x Cons! Thanks! Paulo ASP .Net 3 12-04-2008 03:00 AM
differences between Spring WebFlow,Spring MVC,and String Portlet MVC? rmn190 Java 2 01-10-2008 02:27 AM
Sony MVC CD200 digital still camera questions Paul O. Digital Photography 0 06-14-2006 11:48 PM



Advertisments