MooHa
MooHa

Reputation: 849

MVC Convention in Java Swing Application and JavaBeans

I am curious as to if a VIEW class of the MVC convention can retrieve a bean object to change its textual displays. I am not treating the bean as my model, rather model changes the state of the bean which could then be used by VIEW. On request, Model would pass an object of Bean to the controller who would then pass it it to Views. Is this bad?

Heres What I mean: Bean:

    public class Bean{

    private String eventName;
    private String eventDate;
    //getters
    //setters

}

Model:

public class Model {

Bean e = new Bean();
e.setEventOpportunity(rs.getString(7));
        e.setEventMoreDetails(rs.getString(8));
        e.setEndTime(rs.getString(9));



}

Control:

public class Control () {
public Control() {
Bean events; 
events = model.getEvent(tableClick); //tell model to change its state based on user input on views 
view.changeDisplay(events);
                    }

        }
        } 

Views:

     public class Views {
    public void editTextFields(Bean e) {
       try {
       txtEventName.setText(e.getEventName());
       String dateString = e.getEventDate();


       } catch(Exception te) {
           te.printStackTrace();
       }
   }



    }

is such design keep to the mvc convention still. I didnt want my Model to be cumbersome because its dealing with databases and by putting Bean properties in there the class will get cumbersome.

Upvotes: 1

Views: 349

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328644

You're trapped by the MVCs "editor mismatch", i.e. you have an editor/view and a model and the two can't agree what information to expose how. Usually, the editor/view needs additional information which the model doesn't care about.

Model View ViewModel (MVVM) solves this and the solution is pretty similar to yours.

The only change I suggest is to move the "create Bean" code from the model to the controller. Controllers are meant to configure model, view and view models to play well together.

Upvotes: 2

Related Questions