I’m following this MVC model:
http://java.sun.com/developer/technicalArticles/javase/mvc/
In my model I have an “ArrayList shapes” field and I need the shapes in my view.
Is the only way of getting my shapes by getting them in the modelPropertyChange method?
public void modelPropertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(MyController.PROPERTY_TEXT)) {
ArrayList<Shape> shapes = (ArrayList<Shape>) evt.getNewValue();
}
}
or should I also create a generic getter method in my controller? like this generic setter method:
protected void setModelProperty(String propertyName, Object newValue) {
for (AbstractModel model: registeredModels) {
try {
Method method = model.getClass().
getMethod("set"+propertyName, new Class[] {
newValue.getClass()
}
);
method.invoke(model, newValue);
} catch (Exception ex) {
// Handle exception.
}
}
}
If I need such a generic getter method, I have no clue how to transform this generic setter above to a generic getter method.
If I don’t need such a generic getter method to retreive my data from the model, if I only need the modelPropertyChange method from my View. How would I get my data from the model the first time the application starts? 😮
Pfft I need to get my arraylist of shapes from my model in my view 🙁 (and later I need to get some other data also) So confusing 🙁
Typically with MVC the View will call getters in a Control class. If the data that you are retrieving is ready to be used by the View then the getter in the Control class is usually just a delegate method that calls the appropriate getter in the model; however, if the data being retrieved from the model needs some computation performed on it before it is ready to be displayed, the View would call a getter from a Control class which would call the getter from the Model, perform the required computation, and finally return to the View. By the sound of your question this is the part that you wanted clarification on.
Hope this helps 🙂