Quick Index
Video


Here is a video to go along with this example.

The video works the example end-to-end.

Overview


This example uses a minimal MVC framework.

We will often be coding with the assistance a frameworks.

A framework provides much of the needed functinality as-is.

You will only need to change/tweak a few points in the framework to customize it to fit your model.

RectangleView Tweaks


This section shows a few points in the RectangleView class where you'll want to tweak the code to customize it to fit your model.

First rename RectangleView using your model class name -- e.g. PointView, EllipseView, etc.

Constructing Model
You will want to customize this code (in the constructor) to construct your model, and assign an appropriate label.
		super("Rectangle View");
		controller = new RectangleController(new Rectangle(), this);
Creating Fields
You will want to customize this code (in the constructor) so it works for your model.

createField:

createField(this, "widthField", "Width", true)

  • this -- this view
  • widthField -- name of field (coder's choice; typically named somewhat similar to a model value)
  • Width -- label of field (coder's choice)
  • true/false -- true if the field is editable, false if the field is read-only
		JPanel fields = createVertGroup();
		fields.add(createField(this, "widthField", "Width", true));
		fields.add(createField(this, "heightField", "Height", true));
		fields.add(createField(this, "areaField", "Area", false));
		fields.add(createField(this, "perimeterField", "Perimeter", false));
		fields.add(createField(this, "diagonalField", "Diagonal", false));
		fields.add(createMultiLineField(this, "summaryField", "Summary", false,
				new Dimension(200, 200)));
Action Buttons
Add one or more buttons in this method.

The example is adding one button named "calculateButton".
	private JPanel createButtonGroup()  {
		JPanel buttons = createHorizGroup();
		JButton button = new JButton("Calculate");
		button.setName("calculateButton");
		buttons.add(button);
		return buttons;
	}


RectangleController Tweaks


This section shows a few points in the RectangleController class where you'll want to tweak the code to customize it to fit your model.

First rename RectangleController using your model class name -- e.g. PointController, EllipseController, etc.

Events
This is where you hook up your action button(s).

The example here hooks up the "calculate" button to the method "clickedGo".

You may be happy with this method as-is, depending on your model.
	protected void hookUpEvents()  {
		JButton button = (JButton) this.get("calculateButton");
		button.addActionListener(evt -> this.clickedGo());
	}
Refresh
You will want to customize this code so it works for your model.

The example lines up with Rectangle properties. You'll need to change it so it lines up with your model's properties.

Also see "Concepts" page.
	/** refresh the view from the model */
	public void refresh()  {
		setFieldValue("widthField", this.getModel().getWidth());
		setFieldValue("heightField", this.getModel().getHeight());
		setFieldValue("areaField", this.getModel().getArea());
		setFieldValue("perimeterField", this.getModel().getPerimeter());
		setFieldValue("diagonalField", this.getModel().getDiagonal());
		setMultiLineFieldValue("summaryField", this.getModel().getSummary());
	}
Apply
You will want to customize this code so it works for your model.

The example lines up with Rectangle properties. You'll need to change it so it lines up with your model's properties.

Also see "Concepts" page.
	/** apply the view values to the model */
	public void apply()  {
		this.getModel().setWidth(getFieldValueAsInteger("widthField"));
		this.getModel().setHeight(getFieldValueAsInteger("heightField"));
	}


Running the App


RectangleView is the class that you want to run.

So you can run it:


Using the Example Code as a Template


To get started:


Revise the code as needed for your project:


When done, you can run FarmView in the normal way.

Download code


The code can be downloaded here