Commit b6d3d4a6 authored by Daniel Vogel's avatar Daniel Vogel
Browse files

better modellistener method

parent 1487ab31
...@@ -21,13 +21,9 @@ public class Main { ...@@ -21,13 +21,9 @@ public class Main {
// create View, tell it about model // create View, tell it about model
View view = new View(model); View view = new View(model);
// tell Model about View's listener inner class
model.addView(view.modelListener);
// create second view ... // create second view ...
View2 view2 = new View2(model); View2 view2 = new View2(model);
// tell Model about View's listener inner class
model.addView(view2.modelListener);
// create a layout panel to hold the two views // create a layout panel to hold the two views
JPanel p = new JPanel(new GridLayout(2,1)); JPanel p = new JPanel(new GridLayout(2,1));
......
...@@ -17,8 +17,6 @@ class View extends JPanel { ...@@ -17,8 +17,6 @@ class View extends JPanel {
// the model that this view is showing // the model that this view is showing
private Model model; private Model model;
// model listener
public IView modelListener;
public View(Model model) { public View(Model model) {
...@@ -35,12 +33,13 @@ class View extends JPanel { ...@@ -35,12 +33,13 @@ class View extends JPanel {
this.model = model; this.model = model;
// anonymous class acts as model listener // anonymous class acts as model listener
modelListener = new IView() { this.model.addView(new IView() {
public void updateView() { public void updateView() {
System.out.println("View: updateView"); System.out.println("View: updateView");
button.setText(Integer.toString(model.getCounterValue())); button.setText(Integer.toString(model.getCounterValue()));
} }
}; });
// setup the event to go to the "controller" // setup the event to go to the "controller"
// (this anonymous class is essentially the controller) // (this anonymous class is essentially the controller)
......
...@@ -14,9 +14,6 @@ class View2 extends JPanel { ...@@ -14,9 +14,6 @@ class View2 extends JPanel {
private Model model; private Model model;
private JLabel label = new JLabel(); private JLabel label = new JLabel();
// model listener
public IView modelListener;
View2(Model model) { View2(Model model) {
// create UI // create UI
setBackground(Color.WHITE); setBackground(Color.WHITE);
...@@ -26,16 +23,16 @@ class View2 extends JPanel { ...@@ -26,16 +23,16 @@ class View2 extends JPanel {
this.model = model; this.model = model;
// anonymous class acts as model listener // anonymous class acts as model listener
modelListener = new IView() { this.model.addView(new IView() {
public void updateView() { public void updateView() {
System.out.println("View2: updateView"); System.out.println("View2: updateView");
// just displays an 'X' for each counter value // just displays an 'X' for each counter value
String s = ""; String s = "";
for (int i=0; i< model.getCounterValue(); i++) s = s + "X"; for (int i=0; i< model.getCounterValue(); i++) s = s + "X";
label.setText(s); label.setText(s);
} }
}; });
// setup the event to go to the "controller" // setup the event to go to the "controller"
// (this anonymous class is essentially the controller) // (this anonymous class is essentially the controller)
addMouseListener(new MouseAdapter() { addMouseListener(new MouseAdapter() {
......
...@@ -5,7 +5,7 @@ import java.util.ArrayList; ...@@ -5,7 +5,7 @@ import java.util.ArrayList;
import javax.swing.undo.AbstractUndoableEdit; import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.UndoableEdit; import javax.swing.undo.UndoableEdit;
public class TriangleModel extends Object { public class TriangleModel {
/* A list of the model's views. */ /* A list of the model's views. */
private ArrayList<IView> views = new ArrayList<IView>(); private ArrayList<IView> views = new ArrayList<IView>();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment