开发者

Java Swing MVC question

开发者 https://www.devze.com 2022-12-24 10:02 出处:网络
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.

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[] {
                          开发者_JS百科                        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? :o

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 :-)


I must be totally misunderstanding what you're trying to do because I don't see why you don't just have a simple getter in the model. Then call this method from you view. This assumes your view has a reference to the model. The doc you linked shows the model having getters/setters like this.

ie:

public ArrayList<Shape> getShapes()
{
   // Return my private field.
   return shapes;
}


If you had a look on the link I gave you then you would know that I am using the "Modified MVC"

The only models I see in the view are these:

opacitySpinner.setModel(new SpinnerNumberModel(100, 0, 100, 1));
        opacitySlider.setModel(new DefaultBoundedRangeModel(100, 0, 0, 100));

The only getters I see in the view are being accessed within this view method:

public void modelPropertyChange(final PropertyChangeEvent evt) {
...
}

like: String newStringValue = evt.getNewValue().toString();

So I don't see how to access that getter on my model from within my view. :s If you have a look on the figure in that link, you would see that there is no connection between the model and the view it all happens through the controller. :s

Or am I so wrong :s

(sorry for posting an answer instead of a comment, but there was no codeformatting and limited chars)

0

精彩评论

暂无评论...
验证码 换一张
取 消