Originally posted by John Wells:
I (naively?) assume the JPanel containing the button would need a reference to the controller and would call some method on the controller. . . .
You need a reference in order to invoke a method (unless you use static methods, of course). What I suggest is not having a reference to the controller class, but a listener interface that the controller implements. This isolates the classes from being dependent on each other.
If the controller simply listened for actionevents from the button, it would have no way of knowing the calculation result.
Correct. Part of my suggestion is to have the calculator resolve the action event, then use a custom event to inform the controller. Conversely to the above isolation, having a custom event insures that any class which registers as a listener to the calculator panel will know how to handle the event (as opposed to the more common ActionEvent).
An example gets real complex real quick because we aren't talking basic Swing here. We are trying to create easily maintained code by making independent software components (each possibly consisting of many UI classes, models and controllers) and using a controller class to coordinate them.
I knew we'd discussed this before:
Swing Interapp Communication [ December 20, 2004: Message edited by: Joe Ess ]