Miles said:
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.