File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Is this MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Is this MVC" Watch "Is this MVC" New topic
Author

Is this MVC

Fred Barnes
Ranch Hand

Joined: Jun 11, 2002
Posts: 62
public class View extends JFrame implements Observer {
private JLabel label;
private JTextField field;
public View() {
label = new JLabel();
field = new JTextField(10);
JButton change = new JButton("Change!");
change.addActionListener(new Controller(this));
JPanel panel = new JPanel();
panel.add(label);
panel.add(field);
panel.add(change);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
this.getContentPane().add(panel);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
View v = new View();
}
/**
* @see java.util.Observer#update(java.util.Observable, java.lang.Object)
*/
public void update(Observable arg0, Object arg1) {
label.setText(((Model) arg0).getValue());
}
public String getValue() {
return field.getText();
}
}
public class Controller implements ActionListener {
private View view;
private Model model;
public Controller(View view) {
this.view = view;
model = new Model();
model.addObserver(view);
model.setValue("Fred");
}
public void actionPerformed(ActionEvent ae) {
model.setValue(view.getValue());
}
}
Fred Barnes
Ranch Hand

Joined: Jun 11, 2002
Posts: 62
Sorry I made a mistake! Here follws the source...
public class View extends JFrame implements Observer {
private JLabel label;
private JTextField field;
public View() {
label = new JLabel();
field = new JTextField(10);
JButton change = new JButton("Change!");
change.addActionListener(new Controller(this));
JPanel panel = new JPanel();
panel.add(label);
panel.add(field);
panel.add(change);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
this.getContentPane().add(panel);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
View v = new View();
}
public void update(Observable arg0, Object arg1) {
label.setText(((Model) arg0).getValue());
}
public String getValue() {
return field.getText();
}
}
public class Controller implements ActionListener {
private View view;
private Model model;
public Controller(View view) {
this.view = view;
model = new Model();
model.addObserver(view);
model.setValue("Fred");
}
public void actionPerformed(ActionEvent ae) {
model.setValue(view.getValue());
}
}
public class Model extends Observable {
private String value;

public Model() {
}
public Model(String value) {
this.value = value;
setChanged();
notifyObservers();
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
setChanged();
notifyObservers();
}
}
Regards
Fred
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Yes, that looks right. Essentially, you have two types of notifications when it comes to model updating the views, -- a "push" and a "pull".
A "push" is when you broadcast a message to views and your data is already included in your message:
update(updateType, data)
This way, the views don't need to query the model.
A "pull" is when the broadcast is light, -- all it contains is the update type and it is up to the views to decide which data to pull from the model:
update(updateType)
Eugene
Aruna Raghavan
Ranch Hand

Joined: May 14, 2002
Posts: 194
Eugene,
Java API for Observed has only this:
public void update(Observable o, Object arg)
But how do I pass the updateType with this?


Aruna A. Raghavan<br />SCJP, SCJD, SCWCD
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

Java API for Observed has only this:
public void update(Observable o, Object arg)
But how do I pass the updateType with this?

The most flexible solution is NOT to use the predefined Observer/Observable, but your own equivalent of it. It's realy quite easy, in fact the Observer is just that:

The observable is just your model class that has the means to add, delete, and notify the observers:

Eugene.
[ January 07, 2003: Message edited by: Eugene Kononov ]
Aruna Raghavan
Ranch Hand

Joined: May 14, 2002
Posts: 194
Eugene,
It was real nice of you to post the code. I expected to hear something like
"Well, implement your own" and nothing more
Thanks,
Aruna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this MVC