• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Is this MVC

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eugene,
Java API for Observed has only this:
public void update(Observable o, Object arg)
But how do I pass the updateType with this?
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 promise I will be the best, most loyal friend ever! All for this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic