Since individual controllers are usually added to components, the controller code must have access to a reference to the component it will control.
There are several possibilities. The example has this organization:
- A separate class for the component which inherits from JComponent
- An application whose main method:
- creates the component in a final local variable
- defines an inner controller class
- adds the controller to the component
- creates a frame to view the component
- adds the component to the frame
- makes the frame visible
This approach can make the main method become quite bloated.
Instead of having the main method of the application create the frame AND create the components AND add listeners to the components, we can use inheritance to create a subclass of JFrame that handles these responsibilities.
Then the main application can just create an instance of this specialized frame and make it visible.