class A updates class B, class B updates class A

what if you have a scenario like this.. let's say class A has a canvas where i can draw and class B has a JTable where the coordinates of the drawn object in class A is recorded.. what i want to do is if i draw an object in class A, class A will update the JTable in class B and if i change the values in the JTable in class B, i want class B to update the canvas in class A..

how would you solve a problem like this?

[431 byte] By [hardcodera] at [2007-11-27 11:29:20]
# 1

Look at the Observer-Observable pattern.

java.util.Observer, java.util.Observable.

ordinary_guya at 2007-7-29 16:27:19 > top of Java-index,Java Essentials,Java Programming...
# 2

i have read some articles and tutorials about the Observer-Observable pattern. i after reading, i changed my approach to the problem. now, i have lists that contains the objects that will be observed.. class A and class B can both add items to the lists. i want to know if the lists are changed by class A, is it possible to notify class B only and vice versa?

hardcodera at 2007-7-29 16:27:19 > top of Java-index,Java Essentials,Java Programming...
# 3

> i want to know if the lists are changed by class A, is it possible to notify class B only

> and vice versa?

What's wrong with having the changed thing notify all the objects that have been registered as listeners?

Instances of class A can wait until they have been notified of changes even if the change was initiated by a method of class A.

pbrockway2a at 2007-7-29 16:27:19 > top of Java-index,Java Essentials,Java Programming...
# 4

if i have a canvas class that stores the data of the drawn objects on a list (ArrayList). when i draw something, for example a rectangle, the canvas class will store the object in an ArrayList. that ArrayList is also needed to another class that displays the objects's data on a form with tables. from that table i can edit the size of the rectangle, the coordinates, etc. so if i edit it on the form/table, the drawing in the canvas must be updated.. if i drag/rexize the rectangle on the canvas, the data on the forms and tables must also be updated..

now i'm confused.. the ArrayList contain objects that are modified by canvas class and form class.. so if canvas class modifies the ArrayList, canvas class is already knows the change because that's the class that made the modification.. what will be the observable here? the ArrayList?

hardcodera at 2007-7-29 16:27:19 > top of Java-index,Java Essentials,Java Programming...
# 5

Yes from your description it seems like the ArrayList holds the data, and is the thing being observed. Most likely it will be a class that includes a list of objects. It's behaviour will include adding/deleting objects and changing the shape or position of specific objects.

pbrockway2a at 2007-7-29 16:27:19 > top of Java-index,Java Essentials,Java Programming...