MVC Questions

Discussion in 'Java' started by Miles, May 28, 2008.

  1. Miles

    Miles Guest

    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.
     
    Miles, May 28, 2008
    #1
    1. Advertising

  2. Miles

    Stefan Ram Guest

    Miles <> 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/articles/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/articles/architecture/
     
    Stefan Ram, May 28, 2008
    #2
    1. Advertising

  3. Miles

    Mark Space Guest

    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.
     
    Mark Space, May 28, 2008
    #3
  4. Miles

    Mark Space Guest

    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.
     
    Mark Space, May 28, 2008
    #4
  5. 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
     
    Arne Vajhøj, May 28, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. winglite
    Replies:
    1
    Views:
    426
    Natty Gur
    Nov 3, 2003
  2. rmn190
    Replies:
    2
    Views:
    2,367
    Arne Vajhøj
    Jan 10, 2008
  3. Paulo
    Replies:
    3
    Views:
    482
    Peter Bromberg [C# MVP]
    Dec 4, 2008
  4. Martin Gregorie

    MVC design questions

    Martin Gregorie, Feb 10, 2009, in forum: Java
    Replies:
    25
    Views:
    877
    Martin Gregorie
    Feb 13, 2009
  5. Crazy Cat
    Replies:
    1
    Views:
    691
    schepp
    Sep 3, 2009
Loading...

Share This Page