Model View Control help!

Hi!I have a var in Model called "age". From ActionPerformed in Control I want to take the value from a JTextField in View and update age. How do I do this?ActionPerformed is triggered by a JButton..Thx!
[230 byte] By [Mignona] at [2007-10-3 4:03:51]
# 1

Hi,

I guess that in your model you have getters and setters of the variable "age". So when you call your listener you may pass an instance of your view, I guess that your wiew knows your model.

Then in your Control you can update your variable "age".

Hope you'll understand my bad english (I'm french).

Marco_Poloa at 2007-7-14 22:02:57 > top of Java-index,Desktop,Core GUI APIs...
# 2
Great!How do I pass on the instance?Thx
Mignona at 2007-7-14 22:02:57 > top of Java-index,Desktop,Core GUI APIs...
# 3

By your constructor!

Here's one example

class MyControl implements ActionListener{

//attributs

MyView myView;

public MyControl(MiVew miView){

this.myView=miView;

}

public void ActionPerformed(){

this.myView.my_textField.setText(...);

...

}

}

Marco_Poloa at 2007-7-14 22:02:57 > top of Java-index,Desktop,Core GUI APIs...
# 4
:DOf course, so simple when I see it.. :) I'm stuid! ;)Thanx a lot!
Mignona at 2007-7-14 22:02:57 > top of Java-index,Desktop,Core GUI APIs...
# 5

With MVC you should create the model, view and controller in a driver class that creates all the compoents of the MVC and merges them together. You should be passing the model as a parm to the view in the constructor.

Example Driver Class:

public MVCDriverClass {

// constructor and any other methods needed

public void initialize() {

MyModelInterface model = new MyModel();

MyViewInterface view = new MyView();

MyControllerInterface controller = new MyController(mode, view);

model.initialize();

view.initialize();

view.showView();

}

}

Now in your model you need to make sure you are using the Observer pattern to notify all observers of any changes to the data model. Java already implements this for you if you want to use its utils. You simply need to extend the Observable class on your model to make it the subject and implement the Observer interface on each view you want the Subject (Observer) to notify of changes. You will see that the Observer interface has a method named update(Observer o, Object arg). You simply place any code in here that you need to update your UI values with. Let me know if you need any examples.

sloanCBa at 2007-7-14 22:02:57 > top of Java-index,Desktop,Core GUI APIs...
# 6
Sloan,Would you mind expanding your example just a little, I'd like to see what kind of code goes into the controller. I think I've been putting code that should be in a controller object into my view classes. This is maybe why I'm having such
martybradleya at 2007-7-14 22:02:58 > top of Java-index,Desktop,Core GUI APIs...