Commit b6d3d4a6 authored by Daniel Vogel's avatar Daniel Vogel

better modellistener method

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